Skip to content
Advertisement

When should I explicitly add the type annotations in Typescript? [closed]

it’s best not to add annotations when the type system would end up inferring the same type anyway.

https://www.typescriptlang.org/docs/handbook/2/basic-types.html

Please give some examples for when to add the type annotation and when not to.
How do I know when Typescript will be able to determine the type on its own?

Advertisement

Answer

When TS cannot infer a type, it sets it to any. There’s an option noImplicitAny, which tells the compiler to warn you about such cases, so that you can see which types are not inferred and annotate them.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement