-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'Nullable' built-in utility type #39522
Comments
We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes |
I do realize that this is your stance, and I also address it in the issue text and provide arguments as to why I think this should be an exception to that general rule (inconsistency with existing built-in types that have inverses). The primary goal of this issue is to increase consistency, not new functionality:
|
Consistency is a proxy measure, not an endgoal. We don't do things just to increase consistency; we do them to improve developer's experiences. What we've heard from users is that they don't like it when we needlessly add new global types because a) it causes conflicts with theirs and b) we don't always pick the same definition that they were using. |
Alright, that's fair. Again, I know this is your stance and have also seen this reasoning behind declining other proposals for built-in types, but I thought this was different in that it felt like a needed omission as a companion to |
Probably you should reserve some keywords as JS do. |
I've made a PR
Search Terms
Nullable
Suggestion
TypeScript already comes with a
NonNullable
type, defined inlib.es5.d.ts
:I propose that its inverse,
Nullable
, is added to the built-in type utilities.I'm aware that your general position in terms of built-in type utilities is that you don't extend them unless strictly necessary, but I do think this is an important addition for the following reason:
The built-in type utilities generally offer inverse types. For example,
Partial
is the inverse ofRequired
. And,Pick
is the inverse ofOmit
. Because of this, to me it naturally follows thatNonNullable
should have an inverse calledNullable
.Use Cases
For the following examples, nullable is defined as
null
orundefined
, thereby following TypeScripts definition from theNonNullable
utility type.A few examples that comes to mind:
Additionally, it may help with simplifying built-in type definitions:
For example in
lib.es5.d.ts
, thecatch
andthen
declarations for thePromise
interface can be simplified:There are several types, particularly in
lib.dom.d.ts
that could also be simplified by leveraging theNullable
type.Examples
As part of an
InterfaceDeclaration
:As a optional parameter to a function that may also accept
null
:Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: