You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
nullable type alias
optional type alias
nullable built-in type
optional built-in type
Suggestion
I'd like this added to the list of global built-in types, as an inverse of NonNullable<T>:
typeNullable<T>=T|null|undefined;
Use Cases
Sometimes, when I'm coding at the application level, I want a type that can be either null, undefined, or a value, and I don't want to have to pick one.
In library definitions, it's quite common for an API to accept both null and undefined as equivalent.
It would also make for more easily understood errors and autocomplete, as Nullable<{some: "really", complex: "object", type: true} | number> would be a lot faster and easier to read than {some: "really", complex: "object", type: true} | number | null | undefined.
Lodash's _.chunk definitions could just use chunk<T>(array: Nullable<List<T>>, size: number): T[][]. (That file is full of similar types of the form `T | null | undefined.)
There are over 400 results in searching the DTS repo alone for "null | undefined" and "undefined | null". (Some of the results are of unrelated things, but at least 80% of them are of a type like this.)
Chances are, if you've done significant TS development in general applications or if you've done significant type definitions work, you've probably run into something where this could've helped.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code Probably, but not 100% sure.
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
nullable type alias
optional type alias
nullable built-in type
optional built-in type
Suggestion
I'd like this added to the list of global built-in types, as an inverse of
NonNullable<T>
:Use Cases
null
,undefined
, or a value, and I don't want to have to pick one.null
andundefined
as equivalent.Nullable<{some: "really", complex: "object", type: true} | number>
would be a lot faster and easier to read than{some: "really", complex: "object", type: true} | number | null | undefined
.Examples
Just to name a couple right off:
Validator<null | undefined extends T[K] ? Nullable<T[K]> : T[K]>
_.chunk
definitions could just usechunk<T>(array: Nullable<List<T>>, size: number): T[][]
. (That file is full of similar types of the form `T | null | undefined.)"null | undefined"
and"undefined | null"
. (Some of the results are of unrelated things, but at least 80% of them are of a type like this.)Chances are, if you've done significant TS development in general applications or if you've done significant type definitions work, you've probably run into something where this could've helped.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: