Advertisement
Intermediate TypeScript, Never, Unknown & Void Types

How does 'unknown' interact with union types, and why does `unknown | T` always simplify to just 'unknown'?

Short answer

Since 'unknown' is TypeScript's top type — every other type is assignable TO it — combining it with anything in a union absorbs that other type entirely, because 'unknown' already represents 'could be absolutely anything', making any additional union member redundant information.

Save TypeScript mock interview
Advertisement
AModel answer
Since 'unknown' is TypeScript's top type — every other type is assignable TO it — combining it with anything in a union absorbs that other type entirely, because 'unknown' already represents 'could be absolutely anything', making any additional union member redundant information.
type A = unknown | string; // simplifies to just 'unknown'
type B = string | unknown | number; // simplifies to just 'unknown'
Real-world example
Understanding why accidentally including 'unknown' in a union type silently widens the whole type, discarding your more specific members.
Likely follow-ups
  • How does 'any' behave differently from 'unknown' when combined into a union this way?
Related topics