-
Notifications
You must be signed in to change notification settings - Fork 12.8k
exactOptionalPropertyTypes
does not include undefined
in the type for assignment operators like ??=
#53305
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
Comments
Same as the other one |
@RyanCavanaugh I think this is different from #53306; this one is more about consistency. It appears the left-hand side of an augmentation operator ( In the examples above, for |
I guess I disagree with the OP about where the problem in this sample is. In all cases where an expression is an assignment target, the intended behavior is to show the write type, which is actually neither the pre-assignment CFA type nor the post-assignment CFA type. So in xx.prop += 1;
// xx.prop: number | undefined This is marked in the repro as β
, but the bug is that this is the wrong one, it should be Conversely, this one xx.prop ??= 1;
// ^?
// typeof xx.prop === number β is actually the intended behavior, it's β |
Agreed, under EOPT it should be |
The thing I'm looking at is whether nor not TS can have a read and a write type? But based on what Ryan has said - it sounds like the type of the left expression that we are seeing is the write type - hence it isn't Originally I had assumed the read/write differentiation was done as part of the assignment checking logic - but from what I'm reading here that's incorrect, and instead TS resolves the type to be the write type and then the assignment logic doesn't need to worry about the difference. Which is fair, but does suck for us as we don't have the infra to be able to understand that we could have an undefined in other cases. |
Bug Report
π Search Terms
assignment operator, noUncheckedIndexedAccess
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
TS reports the type of
xx.prop
to be justnumber
when using the??=
assignment operator.This is incorrect -
xx.prop
might beundefined
until AFTER the assignment and also is different behaviour compared to other assignment operators and different to how the expanded assignment is typed.π Expected behavior
TS reports the type of
xx.prop
to benumber | undefined
before the assignment.Additional info
In addition to the above - this behaviour makes it difficult for us to write lint rules because when we attempt to check if the LHS of the assignment is
undefined
, the type system reports that it is not. Example issue typescript-eslint/typescript-eslint#6635The text was updated successfully, but these errors were encountered: