Type Annotations
On variables declared using var
, let
and const
, it is possible to optionally add a type:
TypeScript does a good job of inferring types, especially when simple one, so these declarations in most cases are not necessary.
On functions is possible to add type annotations to parameters:
The following is an example using a anonymous functions (so called lambda function):
These annotation can be avoided when a default value for a parameter is present:
Return type annotations can be added to functions:
This is useful especially for more complex functions as writing expliciting the return type before an implementation can help better think about the function.
Generally consider annotating type signatures but not the body local variables and add types always to object literals.