Reputation: 702
I want to check if a type is nullable or not, and if it has a conditional type on the value.
I tried implementing
type IsNullable<T> = T extends null ? true : false;
However, it does not seem to work
type test = IsNullable<number> // Returns false as it should
type test = IsNullable<number | null> // Returns false when it should be true
What's the proper way of checking if a type is nullable? I tried with T extends null | T
and did not work either.
Upvotes: 4
Views: 2991
Reputation: 44096
You can switch the left and right side of the extends
, so
type IsNullable<T> = null extends T ? true : false;
should work for you.
Upvotes: 10