The primitive types
All primitive types are referenced in lowercase.
number
,string
,boolean
,undefined
,null
...TS adds a couple of extra lowercase types solely related to its type-checking job:
any
,unknown
,void
,never
...Arrays can be declared either by
something[]
orArray<something>
;
Be careful: There also exists an uppercase
Number
type, which is a different thing from lowercasenumber
! Types likeNumber
,String
,Boolean
refer to the javascript functions that have those names.Be careful: Both types
{}
andobject
refer to an empty object. To declare an object that can receive any property, useRecord<string, any>
.
Strict nulls
Unlike some other languages, types do not implicitly include
null
;Ex: in Java, any variable can always also be null;
In TypeScript a type is declared as nullable through a type union:
type X = Something | null | undefined
A type can be narrowed as "not null" through control flow analysis. Ex:
You can tell the compiler to assume a variable is not null with the
!
operator;
Last updated