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>.

GroupUtility Types
KindType
Strict modeNot 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

TThe 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏