During my search for a Nullable type in TypeScript, I stumbled upon the NonNullable
type in the file path:
C:\Program Files\Microsoft VS Code\resources\app\extensions\node_modules\typescript\lib\lib.es6.d.ts
The definition of NonNullable
is as follows:
/**
* Exclude null and undefined from T
*/
type NonNullable <T> = T extends null | undefined ? never : T;
I'm curious to understand what this definition means and how the ?
operator and the never
keyword apply in the context of generic constraints. Is there any documentation available that explains this in detail?
Additionally, I came across similar definitions within the same file:
/**
* Exclude from T those types that are assignable to U
*/
type Exclude<T, U> = T extends U ? never : T;
/**
* Extract from T those types that are assignable to U
*/
type Extract<T, U> = T extends U ? T : never;