-
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
Suggestion: Add Nullable<T> and Optional<T> to lib.d.ts #19944
Comments
I'm still not sure I see a huge benefit to adding it for everyone. Here's a few reasons why
Note that I do understand a general desire to have these coming from other languages that have a core set of utility types built in, but overall, I don't think think the lack of these aliases has been a major problem in TypeScript. |
It isn't about fewer characters, and I would agree that the lack of these aliases is not a major problem. To me it just seems that Idunno, maybe it is just a bias coming from other languages but it just feels like these are missing things that should be there. |
I think part of the challenge though would be that some people would assume that if you use |
The conclusion a while back in #7426 was that Again, if you find these aliases helpful, you are more than welcome to define them locally and use them. our experience however does not suggest there is a need for them in the wider community at the time being. |
There have been a number of issue threads discussing adding nullable types (most notably #7426 and #7488) and the idea of adding them to the core TypeScript Language was ultimately rejected.
However, it was also suggested in that issue that type mappings could be added to lib.d.ts
But then the issue was closed and the suggestion seems to have been dropped. (see #7426 (comment) by @mhegazy )
Nullable<T>
is even mentioned in https://www.typescriptlang.org/docs/handbook/advanced-types.html alongsidePartial<T>
, as well asReadonly<T>
,Pick<T, K extends keyof T>
, andRecord<K extends string, T>
. It is confusing when reading that document thatNullable<T>
isn't in the core library and needs to be added to your project if you would like to use it.To me it seems like it should be a logical and uncontroversial choice to add these type mappings. They are tiny additions to the library, and they provide a nicer syntax (IMHO) than explicitly using
T | null
orT | undefined
.The text was updated successfully, but these errors were encountered: