TypeScript defines a strict type for each variable and ensures you use that type throughout your code. Even if you do not define a type, TypeScript will automatically do this for you (implicit typing).
let variable = "hello"; // will have a type of string and "variable" cannot be assigned any other data type
let age = 12; // Will have a type of number and "age" cannot be assigned to any other data type
let ageWithType: number;
ageWithType = 55;
let anotherAgeWithType: number = 33;
let myType; // has a type of any - it can be a string, boolean, number, or object. Not using TypeScript effectively
let testString: string = "Hello";
let testBoolean: boolean = true; // can be either true or false
let testNumber: number = 42;
TypeScript allows us to use more than one data type for a variable or a function parameter without causing type errors.
let testStringOrNumber: string | number;
testStringOrNumber = "2";
testStringOrNumber = 4;
let names = ["John", "Jane", "Tom"]; // will have a type of string[]
let numbers = [1, 2, 3.4]; // Will have the type of number[]
let testStringArray: string[];
testStringArray = ["Hello"];
let testNumberArray: number[];
testNumberArray = [4, 5, 6];
let testStringOrNumberArray: (string | number)[];
testStringOrNumberArray = [1, "one", 2, "two"];
In the example below, each property on the user object has its own implicit type (username: string, age: number, isAdmin: boolean). If any properties are added or missing when using this object, errors will occur. However, you can modify existing data within the object, provided it is the same type.
let user = {
username: "John",
age: 22,
isAdmin: false,
};
// Correctly modifying a TypeScript Object
user.username = "ABC123"; // This is acceptable to do, as you are not changing the data type
// Incorrectly modifying a TypeScript object
user.isAdmin = "true" // will cause a type error as you are trying to change the type of the property
user.eyeColor = "blue" // Will throw an error because the property doesn't exist at the time of creating the type
let userObject: {
username: string,
age: number,
isAdmin: boolean,
};
userObject = {
username: "John",
age: 22,
isAdmin: false,
};
To add optional parameters to objects, you add a question mark after defining the property name. Properties that follow this syntax will not throw errors if they are not included in the object.
let userObject2: {
username: string,
age: number,
isAdmin: boolean,
phoneNumber?: number,
};
userObject2 = {
username: "John",
age: 22,
isAdmin: false,
// Throws no errors before adding phoneNumber property as I have made it optional
phoneNumber: 123456789,
// Throws no errors after it's been added, provided it has the correct type assigned to it
};