Here's an example of a TypeScript type:
type NonNullable<T> = T extends null ? never : T
type Test1 = NonNullable<string>; // string
type Test2 = NonNullable<string | null>; // still string!
The ternary operator is applied individually to each member of the T
union.
Now, I'd like to create a type that looks at union T
as a whole, like this:
type NonNullable2<T> = ...?
type Test1 = NonNullable<string>; // string
type Test2 = NonNullable<string | null>; // never, because the provided T (string|null) extends null.
So, if I provide any T
that can be null, I want to get never
. Any other type should return T
.
How can I do that?