The code explains my question:
type A = {
a: number,
} | null
// Extract as defined in lib.es5.d.ts
type Extract<T, U> = T extends U ? T : never;
type CustomExtract = A extends null ? A : never;
type Result1 = Extract<A, null> // null
type Result2 = CustomExtract; // never
Extract and CustomExtract are the same code, with the difference that Extract is a generic type.
Also, as related example, string | null
does not extends null
.
So, how types really works under the hoods at this topic? I can imagine it maybe runs the generic type for each type of the union and then unionize all the results, but I want the real technical definition and working of it.
See Question&Answers more detail:os