With ‘type’ in its name, Typescript lives up to its reputation of being a typed language. But, what exactly are the supported types?
At a high-level we can classify types as built-in (basic & special ) and additional types including user-defined types.
Built-in Types (Basic)
Similar to Javascript implicit types, we have number
, string
, boolean
to form the three core built-in types.
|
|
We also have null
, and undefined
as additional basic built-in types.
|
|
You could technically do this..
|
|
.. and also this (when a flag called --strictNullChecks
is not specified during compilation) -
|
|
Oh, and symbol is also a valid type.
|
|
Built-in Types (Special)
There are four types that have special function within Typescript.
any
any
represents any type. It gives super powers to Typescript and makes it play well with Javascript. any
tells Typescript to do a type inference on a variable.
For e.g. ,
|
|
any
can be your best friend. You can convert your entire Javascript to Typescript by simply changing extension and continue using it as before. You could then incrementally add Typescript powers to the script.
Since any variable without a type is assumed as type any
, Typescript can also interact Javascript without a hitch.
unknown
unknown
was introduced in Typescript v3. It is type safe any
- a variable with type unknown
can be assigned value of any type.
|
|
But, unknown
cannot be assigned to any other type other than unknown
.
|
|
.. and, no operations are allowed for unknown data type against a known type.
|
|
void
As you expect, void
has its duty cut out with functions - it primarily means no type.
|
|
void
can also be used against mere variables, but you would have to answer to yourself whether you are addressing a galaxy-level problem doing that. These variables are not assignable to valid values thereon.
|
|
never
never
signifies values that never occur. That can be confusing if you’re not into quantum mechanics, but let me explain.
Consider a function that always throws error - it does not need to return anything.
|
|
So, how can it distinguish itself from void
?
void
is for functions where you can reach the end of function. While never
is for functions where end of function is never reached.
|
|
Additional Types
‘Additional type’ is just a bucket to include all the other types other than the primitive types defined above. Typescript supports other types including user-defined types like those described below.
Collections
Collections can include arrays and tuples. Each of those collections can have elements with a specific type or a mix of types using any
.
|
|
A tuple with fixed number of elements has those elements with specific types.
|
|
Special data type: enum
enum
is not like anything that you have seen so far.
- you define
enum
- a variable of the defined
enum
type can be assigned to a value within theenum
Consider the below example -
|
|
Object
As in Javascript, anything that is not primitive is an object.
The following code is valid..
|
|
But, depending on your situation, may not be useful for type assertion against the object’s props (like a Array