Домой United States dc3 United States software in english pdf How the TypeScript NonNullable Type Works

How the TypeScript NonNullable Type Works

331
0
ПОДЕЛИТЬСЯ

The NonNullable type is a utility type in TypeScript which creates a new type, whilst removing all null or undefined elements.
Join the DZone community and get the full member experience. 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. This article covers custom types. To learn more about custom types, read my guide about it here. 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:
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:
In the above example, noNulls is now of type string| number. Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here. Opinions expressed by DZone contributors are their own.