any
is compatible with any and all types in the type system, meaning that anything can be assigned to it and it can be assigned to anything. It gives you the power to opt-out of type checking.void
is the absence of having any type at all. It is commonly used as the return type of a function that do not return a value.never
type represents the type of values that never occur. For instance, never
is the return type of a function which will always throw an exception or not reach its end point.undefined
and null
actually have their own types named undefined
and null
, respectively. Much like void
, they’re not extremely useful on their own but they become useful when used within union types (more on that in a bit)any
. Anything is assignable to unknown
, but unknown
isn’t assignable to anything but itself and any.
No operations are permitted on an unknown
without first asserting or narrowing to a more specific type.?
, meaning that objects of the interface may or may not define these properties.enum
(or enumeration) is a way to organise a collection of related values that can be numeric or string values.enum
values start from zero and increment by 1 for each member.enum
supports reverse mapping which means we can access the value of a member and also a member name from its value.public
,private
,protected
modifiers, which determine the accessibility of a class member.public
member works the same as plain JavaScript members and is the default modifier.private
member cannot be accessed from outside of its containing class.protected
member differ from a private as it can also be accessed within deriving classes.readonly
property must be initialised at their declaration or in the constructor.as
instead.x
is compatible with y
if y
has at least the same members as x
.y
has a member name: string
, it matched the required properties for the Person interface, meaning that x
is a subtype of y
. Thus, the assignment is allowed.x
might be a boolean and the function toFixed
cannot be called on it.in
operator checks for the existence of a property on an object.A
is assignable to type B
, then X
is the same type as C
. Otherwise X
is the same as type D;
keyof
operator queries the set of keys for a given type.T
.keyof T
represents the union of all property names of T
as string literal types.[P in keyof T]?: T[P]
denotes that the type of each property P
of type T
should be optional and transformed to T[P]
.T[P]
represents the type of the property P
of the type T
.Readonly
type that takes a type T
and sets all of its properties as readonly.Exclude
allows you to remove certain types from another type. Exclude
from T
anything that is assignable to T
.Pick
allows you to pick certain types from another type. Pick
from T
anything that is assignable to T
.infer
keyword to infer a type variable within the extends
clause of a conditional type. Such inferred type variable can only be used in the true branch of the conditional type.MyReturnType
:T
is …T
a function?R
.any
.