Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 28, 2022 04:10 pm GMT

How the TypeScript Parameters Type Works

The NonNullable type is a utility type in TypeScript which 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 which accepts null and undefined as potential options:

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:

type myType = string | number | null | undefinedtype noNulls = NonNullable<myType>

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


Original Link: https://dev.to/smpnjn/how-the-typescript-parameters-type-works-12h4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To