Skip to main content
The unknown type is a type-safe top type in TypeScript’s type system. It represents a value that can be of any type, but unlike the any type, it enforces strict compiler checks by prohibiting arbitrary operations, property access, or method calls until the value’s specific type is resolved.

Assignability

Because unknown is a top type, all other types are assignable to it.
Conversely, unknown is strictly constrained in outbound assignability. A variable of type unknown can only be assigned to variables of type unknown or any.

Operational Constraints

The TypeScript compiler treats unknown as a completely opaque value. You cannot perform operations on it, access its properties, or invoke it as a function or constructor.

Type Resolution

To interact with an unknown value, you must resolve its type using either control flow analysis (type narrowing) or type assertions. 1. Type Narrowing Using type guards (typeof, instanceof, or custom type guard functions) allows the compiler to narrow the unknown type to a more specific type within a conditional block.
2. Type Assertions If the type is known to the developer but not the compiler, a type assertion can be used to override the unknown constraint.

Union and Intersection Mechanics

When combined with other types in unions and intersections, unknown follows specific algebraic rules: Union Types A union of unknown and any other type (except any) evaluates to unknown. It absorbs other types because unknown already encompasses all possible values.
Intersection Types An intersection of unknown and any other type evaluates to the other type. Since unknown places no structural constraints on a type, intersecting with it does not alter the intersected type.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More