JS Built-ins
TypeScriptbeginner

Utility Types β€” NonNullable<T>

πŸ“– Usage Example
type Id = NonNullable<string | null | undefined>
// Id = string

NonNullable<T> removes null and undefined from a union type, leaving only concrete values.

Utility Types: NonNullable<T>

NonNullable<T> removes null and undefined from a type.

Challenge: Implement getNonNull<T>(value: T): NonNullable<T> that returns the value or throws if it is null/undefined.

getNonNull(42)    // β†’ 42
getNonNull(null)  // throws
πŸ’‘ Hints (1)
  • β€’ Check for both null and undefined explicitly.