Type Annotation in TypeScript
- TypeScript uses type annotations to explicitly specify types for identifiers such as variables, functions, and objects.
- TypeScript uses the syntaxÂ
: type
 after an identifier as the type annotation, whereÂtype
 can be any valid type. - Once an identifier is annotated with a type, it can be used as that type only. If the identifier is used as a different type, the TypeScript compiler will issue an error.
Also consider the following:
For Arrays:
For objects:
This specifies the type of each parameter of the object.
Function Arguments and & Return Types
We can declare functions that set up the return and input type beforehand:
Summary
- Use type annotations with the syntaxÂ
: [type]
 to explicitly specify a type for a variable, function, function return value, etc.