NonundefinedT
NonundefinedT is a type-level utility pattern used in TypeScript codebases to express a version of a type T that excludes the possibility of being undefined. It is not a built-in keyword, but a common alias pattern that helps enforce defined values at compile time.
A typical definition uses a conditional type to remove undefined from T. For example:
type NonundefinedT<T> = T extends undefined ? never : T;
This approach relies on TypeScript’s distributive conditional types to strip undefined from union types.
Examples illustrate how it behaves with unions:
type A = NonundefinedT<string | undefined>; // string
type B = NonundefinedT<undefined | number | null>; // number | null
type C = NonundefinedT<undefined>; // never
Some codebases prefer an alternative alias using Exclude:
type NonundefinedT<T> = Exclude<T, undefined>;
This achieves the same effect by explicitly excluding undefined from T.
- You can use NonundefinedT in function signatures or type annotations to express that a value should
- It leaves other optionalities intact, including null, unless you use a broader utility.
- NonundefinedT focuses on removing undefined; it does not remove null. To remove both, use NonNullable<T> or
- It is related to other TypeScript utility types such as Exclude and NonNullable, which provide broader
- This is a compile-time construct; runtime values can still be undefined. Type correctness does not automatically
- It depends on the TypeScript type system and is not an actual runtime type or value.