Controlling Types in TypeScript
Controlling Types in TypeScript
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It offers type checking to prevent errors, and catches mistakes early before they become bugs. With TypeScript, developers can control the types of variables, objects, functions, etc., using static type annotation.
Types are used to provide hints about the expected behavior of variables and other code elements. By setting types on variables and other pieces of code, it can be ensured that the types used are compatible with each other, and that the code will not fail or produce unexpected results due to incompatible types. Types also help to define a contract for how code should behave, providing a specification of what output should be expected for a given input.
Type Annotations
TypeScript allows you to create type annotations by using the type syntax. This simply adds a type annotation to the variable, object, class, or function you want to specify the type for. You can also create type aliases by using the type keyword and giving it a name. For example, the following code creates a type alias called “Person” that contains the properties of a person:
type Person = {
name: string,
age: number
}
You can then use this type alias when declaring a variable to ensure it has the appropriate properties, as follows:
let person: Person = {
name: 'John',
age: 30
};
Type annotations can also be used to create unions of types, which give you the ability to specify that a value can either be of one type or another. To do this, use the pipe (|) character to separate the types, like so:
let var1: string | number;
In this case, “var1” can be either a string or a number.
Type Guards
Type guards are statements in TypeScript that check the type of a variable. They are used to narrow down the type of a variable within a function or statement. For example, the following code will only execute the code within the “if” statement if the type of the “x” variable is a number:
let x: any;
if (typeof x === 'number') {
// do something
}
Type guards can also be used to check for union types, as in the following example:
let y: string | number;
if (typeof y === 'string') {
// do something
} else if (typeof y === 'number') {
// do something else
}
This code will only execute one of the code blocks depending on the type of the “y” variable.
Type Casting
Type casting can be used to convert a variable from one type to another. In TypeScript, type casting is done by placing the desired type in angle brackets after the variable name, like so:
let z: any;
let zAsString = z;
Now, the “z” variable will be treated as a string, even though its type was originally any.
Conclusion
TypeScript provides powerful type checking features to help you ensure your code is correct and bug-free. Type annotations provide hints to the compiler about the expected behavior of variables and other code elements, while type guards and type casting can be used to control the types of those elements. By taking advantage of these features, you can ensure that your code is accurate and reliable.