When to use `never` and `unknown` in TypeScript
— TypeScript — 1 min read
unknown primitive types were introduced in TypeScript v2.0
and v3.0 respectively. These two types represent fundamental and
complementary aspects of type theory. TypeScript is carefully designed according
to principles of type theory, but it is also a practical language, and its
features all have practical uses – including
understand those uses we will have to begin with the question, what exactly are
Types explained using set theory
When you get down to a fundamental definition a type is a set of possible
values, and nothing more. For example, the type
string in TypeScript is the
set of all possible strings. The type
Date is the set of all instances of the
Date class (plus all structurally-compatible objects), and the type
Iterable<T> is the set of all objects that implement the iterable interface
for the given type of iterated values.
TypeScript is especially faithful to the set-theoretic basis for types; among
other features, TypeScript has union and intersection types. A type like
| number is called a "union' type because it literally is the union of the set
of all strings, and the set of all numbers.
» Read the rest on the LogRocket blog.