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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Trending

  • Go 1.24+ Native FIPS Support for Easier Compliance
  • The Role of AI in Identity and Access Management for Organizations
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  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 user avatar
Johnny Simpson
DZone Core CORE ·
May. 22, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!