-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Nullish Coalescing and Logical Compound Assignments (??=
, ||=
, &&=
)
#37255
Comments
Most likely this one should be unchecked, because this syntax is not supported by ES. However there is proposal https://github.com/tc39/proposal-logical-assignment which is in Stage 2 and TS usually start implementation if proposal reaches Stage 3. |
Hm negation confusion :) I checked this item because "This isn't a runtime feature" as in "This is a compile-time feature" because TSC needs to compile it to something else. Or what else is intended behind this item? Thanks for the TC pointer. Makes sense to extend this to all logical operators! |
It's in a good direction in the standards pipeline. Iif I recall correctly, I was the only one who had some minor objections around tc39/proposal-logical-assignment#3 which I've changed my mind on. We'll look into implementing it when it hits stage 3. |
??=
, ||=
, &&=
)
Could I take a try on this one after stage3?🙋🏻♂️ |
It looks like it'll be discussed tomorrow. I've given my review approval and I can let you know when it moves forward. I've put together a write-up to consider for any implementations: This new proposal adds a few new productions:
These roughly correspond to the following respectively:
An implementation for TypeScript should at least test the following:
|
It just hit stage 3! |
Yes, I saw your approve at tc39😳 |
Is this a bug or am I missing something? let x: { a?: boolean } = {};
x.a ??= true;
x.a &&= false;
^^^ Type 'false' is not assignable to type 'true'.(2322) |
@aminpaks seems a bug. I'll take a look on it. |
@Kingwl is this bug too? I don't see how |
@kolpav I think this is unrelated but caused by the fact that narrowed types do not flow into callback functions. You can workaround that by creating a local |
@danielrentz Thats unfortunate. I would like to keep react props on |
@kolpav I'm in the same situation. I don't see how type NonNullableProp<
TObject extends { [key: string]: any },
TKey extends keyof TObject
> = Omit<TObject, TKey> &
{
[key in TKey]-?: NonNullable<TObject[key]>
}
function assignDefault<TProps>(
props: TProps,
key: keyof TProps,
value: TProps[typeof key],
): asserts props is NonNullableKey<TProps, typeof key> {
props[key] ??= value
}
const Component = (props: {
size?: 'small' | 'medium' | 'large'
}) => {
assignDefault(props, 'size', 'medium')
// From this line, size is not considered optional anymore
// ...
} |
@gustavopch Is exactly something I was trying to avoid
Your solution looks nice but in my case I am not willing to write code doing "nothing" just to please our type checking overlords. TS usually forces you to write better code but this time it doesn't seem to be the case. |
Search Terms
Nullish coalescing assignment
Suggestion
A new operator
??=
to assign some default to a variable/property if it is nullish.It's also avaibale in PHP: https://wiki.php.net/rfc/null_coalesce_equal_operator
Examples
obj1.obj2.obj3.x ??= 42;
instead of
obj1.obj2.obj3.x = obj1.obj2.obj3.x ?? 42;
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: