DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

Trending

  • Every Acquisition Promises One Company. Most Deliver Two.
  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Designing Human-in-the-Loop Approval Gates for Enterprise AI Agents
  • SEO Writing 101: A Simple Guide to Getting Found

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. How the TypeScript NonNullable Type Works

How the TypeScript NonNullable Type Works

Let's take a look at how the TypeScript NonNullable Type works.

By 
Johnny Simpson
·
May. 22, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The NonNullable type is a utility type in TypeScript that creates a new type, whilst removing all null or undefined elements. It lets us take existing types, and modify them so they are more suitable in certain situations. Let's look at how it works.

Custom Types

This article covers custom types. To learn more about custom types, read my guide about it here.

NonNullable Utility Type

The NonNullable utility type works a lot like other utility types, in that it can take an existing type, and modify it as you see fit. As an example, let's say we have a specific union type that accepts null and undefined as potential options:

TypeScript
 
type myType = string | number | null | undefined

This example works great in one example, but there is another part of our code where we don't want to accept null or undefined. We could create a new type for that, or we could reuse myType, using NonNullable:

TypeScript
 
type myType = string | number | null | undefined
type noNulls = NonNullable<myType>

In the above example, noNulls is now of type string | number.

TypeScript

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java to TypeScript in a Day: AI Migration Playbook
  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook