NonNullable<Type>
Excludes null
and undefined
from Type
.
It is useful when you are certain that a value will be present, and you want TypeScript to enforce that.
You can implement NonNullable
by returning the never
type for null
and undefined
.
Be careful not to overuse NonNullable
— it is best applied after you've actually checked for null
/undefined
, or in places where you know the value is safe.