Type Annotation
using the colon symbol followed by the type we want to use.
now there are 3 way to annotate ( note in below example T
is just a type )
if method is there then
Type Define : Explicit or Implicit
Type inference
while it is possible to explicit annotate but rely on inferred type which done by typescript type system
VS Code Inlay Hints does the same
and when you hover on n
, its type is number
; there is no need to set explicit type ( i.e. type annotate :number
)
many occurrences, typescript set explicit any
// we could modify our TSConfig to allow implicit typings any
noImplicitAny: true
Function typing
it is inferred automatically but required when we call function and use some operations.
there are various way to set typing of a function
to simplify we can use separate typing
References
https://typescript.tv/hands-on/type-inference-type-annotations-in-typescript/ https://stackblitz.com/edit/dom-typing