-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat: Nullable<T>
lib type (Taylor's Version)
#51254
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
Conversation
There is already a `NonNullable<T>`lib type, which does not seem specified in ECMA-262 and so should not be a valid basis for rejecting this PR, which I am sure many people will be thankful for as [a good chunk](https://www.google.com/search?q=%22Nullable%3CT%3E%22+includes%3Atypescript) of TS workspaces define this manually, usually as the `T | null` union. This suggestion was resisted in microsoft#19944, presumably because it's hard to know whether to use `T | null` or the example provided under the [**old handbook**](https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types), `{ [P in keyof T]: T[P] | null }` (all properties on `T` assignable to `null`). There is nothing that stops us from doing both, and constructing a utility type `Nullable<T>` which can be assigned either to `null` *or* `T` with all properties assignable to `null`: ```ts type Nullable<T> = null | { [P in keyof T]: null | T[P] }; ``` This is not an existing utility type and so it is not as if it's breaking. This would afford a lot of convenience without any apparent drawbacks as far as I can tell, and the other PR seems to have been closed mostly on the basis of vibes only, not for any real reason. Ultimately it does seem kind of ridiculous to leave a `NonNullable<T>` type without defining a `Nullable<T>` one, and I suggest we take a stab at it.
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
I submitted this lazily through GitHub GUI but I'll fill out the lib sheet if someone confirms they're not just going to throw this in the garbage, which I'm expecting. I know this to not be correct given the versatility of this approach and also the high occurrence of this utility type (usually
|
This was declined already. See #39522. Besides that, I find it very confusing that Also, this is a breaking change for everyone who might already have a global |
If |
A few things. First, we generally don't want to add more helper types to Second, it's been years since #19944 was filed, and I've not seen enough evidence that having an "inverse" of Third, the way this works is not at all how I, nor probably anyone else on the team, would define it. You already have several differing opinions of how this should work, which is what we already experienced in the early discussions around nullability. If you add a feature like Finally
this is another warning - your interaction on the issue tracker is regularly hostile. You cannot keep throwing the maintainers under the bus when you disagree with a design decision. |
There is already a
NonNullable<T>
lib type, which does not seem specified in ECMA-262 and so should not be a valid basis for rejecting this PR, which I am sure many people will be thankful for as a good chunk of TS workspaces define this manually, usually as theT | null
union.This suggestion was resisted in #19944 (and many, many subsequent PRs and issues), presumably because it's hard to know whether to use
T | null
or the example provided under the old handbook,{ [P in keyof T]: T[P] | null }
(all properties onT
assignable tonull
).There is nothing that stops us from doing both, and constructing a utility type
Nullable<T>
which can be assigned either tonull
orT
with all properties assignable tonull
:This is not an existing utility type and so it is not as if it's breaking. It is not equivalent to
T | null
but is a superset of it, so there is more utility baked in than you can describe withT | null
while still getting equivalent behavior for primitives. This would afford a lot of convenience without any apparent drawbacks as far as I can tell, and the other PRs seem to have been closed mostly on the basis of vibes only, despite lots of pushback in the RFC and several issues/PRs opened between 2016 and now.Ultimately it does seem kind of ridiculous to leave a
NonNullable<T>
type without defining aNullable<T>
one, and I suggest we take a stab at it.Fixes: #51255