![]() |
VOOZH | about |
Type annotations in TypeScript are used to explicitly specify the type of a variable, function parameter, or object property. This helps catch errors early, improves code readability, and ensures type safety.
Example:
Output:
string
number
["GFG", "TypeScript", 500, 20]
In this example:
Type annotations in functions let you specify the expected types of parameters and the return value. This improves readability, enforces correctness, and helps catch errors early.
Now let's understand this with the help of example:
Output
Hello, Alice!Type annotations for objects define the structure and types of properties the object must have. This ensures type safety and prevents invalid assignments.
Now let's understand this with the help of example:
Output:
{ name: 'Alice', age: 30 }Array type annotations explicitly specify the type of elements an array can hold. This helps catch errors and improves code readability.
Now let's understand this with the help of example:
Output
[1, 2, 3, 4, 5]Type annotations in classes define the types of properties and method parameters/returns, ensuring that objects of the class follow a consistent structure. This improves type safety and code clarity.
Now let's understand this with the help of example:
Output:
Rectangle { width: 5, height: 10 }
50