NonNullable<T>
Last updated: 2026-10-22
NonNullable<T> NonNullable
NonNullable<T> strips null and undefined from T. It fits cases where a field may be empty in its declaration but is guaranteed non-empty by the time you reach it — after default-value fallback or array filtering, for instance. It is essentially a dedicated alias of Exclude<T, null | undefined>.
| Group | Utility Types |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
type NonNullable<T> = T extends null | undefined ? never : T;
// Usage
type Name = NonNullable<string | null | undefined>; // string
function pickFirst(values: Array<string | null | undefined>): string {
for (const v of values) {
if (v != null) {
const clean: NonNullable<typeof v> = v;
return clean;
}
}
return "(none)";
}
⚙️ Parameters
| T | The source type, usually containing null/undefined members; unions are stripped member by member |
|---|
Returns:A new type: T with null and undefined removed.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
Ada
(none)
(Runtime output after transpilation; at the type level CleanField is string | number — null/undefined are gone, and only values that survive the v != null filter can be assigned to it)
💡 Notes
ℹ️ Info Implementation: type NonNullable<T> = T extends null | undefined ? never : T — the same distributive conditional pattern as Exclude.
💡 Tip It changes types only: runtime cleanup needs v != null filtering, ?? defaults or Array.filter. Pair the type-level tool with runtime cleaning for a complete story.
⚠️ Note Do not use NonNullable to whitewash emptiness you are unsure about: relabeling string | null as string compiles fine and still crashes on null.dosomething at runtime. Handle empty values with runtime checks first.
🏷️ Related Utility Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q How does NonNullable differ from the non-null assertion !?
A Both remove null/undefined, but NonNullable is a type computation (a reusable, composable new type) while ! is an expression-level assertion (in-place, not reusable). Use NonNullable when you need the de-emptied type as a first-class type.
Q Does NonNullable affect boolean?
A No — it targets only the literals null and undefined. NonNullable<boolean | null> yields boolean (including both true and false).
Q Is NonNullable still meaningful with strictNullChecks off?
A With strictNullChecks off, null/undefined assign to everything, so the narrowing is unobservable. NonNullable is only meaningful under strictNullChecks — one more reason to always keep strict mode on.