Sub topic 1
Types as Sets of Values: Top and Bottom Types
TypeScript types can be understood as sets of possible values. A type annotation limits which values a variable may hold.
For example, boolean is the set { true, false }. The literal type "ready" is a smaller set containing one value. A union combines sets, while an intersection describes values that satisfy both types.
This chapter assumes you know basic TypeScript syntax, interfaces, union types, and JavaScript’s typeof and instanceof operators. You should also be able to run the TypeScript compiler with tsc.
Prerequisites
You need basic TypeScript syntax, type annotations, interfaces, and union types. Familiarity with typeof, instanceof, and try/catch is also assumed.
The type hierarchy
Some types sit at the edges of this set-based model:
unknownis the top type. Its set contains every value.neveris the bottom type. Its set contains no values.anyis a special escape hatch that largely disables the set-based checking rules.
The following diagram shows the useful relationship between these types:
A value from a smaller set can be used where a larger set is expected. Every string is also a possible unknown value, so a string can be assigned to unknown.
The reverse is unsafe. An unknown value might be a string, number, object, or null. TypeScript therefore requires you to check it before using it as a specific type.
`unknown`: the safe top type
Use unknown when you have a value but do not yet know its type. This commonly happens at system boundaries such as parsed JSON, API responses, and caught exceptions.
let value: unknown = "hello";
// value.toUpperCase(); // Error: value is unknown
if (typeof value === "string") {
console.log(value.toUpperCase()); // value is now string
}
if (typeof value === "number") {
console.log(value.toFixed(2)); // value is now number
}The typeof checks narrow the set of possible values. Inside the first block, only strings remain possible. Inside the second block, only numbers remain possible.
unknown permits safe operations that work for every value, such as comparison with === or conversion with String(value). It does not permit property access, function calls, or arithmetic until a check proves that the operation is valid.
`any`: outside the model
any allows a value to be assigned to or used as almost any type. It also allows operations without proving that they are valid.
let value: any = "hello";
value.toFixed(); // Compiles, but throws at runtime
const count: number = value; // Compiles without validationThe compiler cannot protect you from the invalid method call or the incorrect assignment. Treat any as a deliberate boundary around legacy code or incomplete migration, not as a general replacement for unknown.
| Type | Values it can represent | Requires checks before use |
|---|---|---|
| `unknown` | Every value | Yes |
| `any` | Almost every value, without useful checking | No, but runtime errors remain possible |
| `never` | No values | Not applicable |
`never`: the empty type
never represents a situation that cannot occur. A function that always throws or never finishes can return never:
function fail(message: string): never {
throw new Error(message);
}
function requiredPort(port: string | undefined): string {
if (port === undefined) {
return fail("PORT is required");
}
return port;
}After fail is called, execution cannot continue normally. TypeScript can therefore treat the remaining path as unreachable.
never also appears in type operations. In a union, it contributes no values, so string | never becomes string. In an intersection, incompatible types can produce no possible value, so string & number becomes never.
The practical difference is clear: use unknown when a value exists but needs validation. Use never when no value is possible. Use any only when you consciously accept the loss of type safety.
Exercise
Which type should you use for a value returned by an unvalidated JSON parser: any, unknown, or never?
Show answer
Use unknown. The value exists, but its shape has not been verified. Narrow or validate it before using it.