-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Literal types and their unions should not be mutable #28077
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
This seems ok to me, though I might be missing something. At the point in the expression b does have type 5, but after the expression the type of b is correctly widened. Do you have an example where this leads to undesirable behaviour? let a: 'a' = 'a';
a += 'x';
const x: 'a' = a; // string not assignable to 'a'
let b: 5 = 5
b += 7;
const y: 5 = b; // number is not assignable to 5 |
I didn't know that, I didn't even expect that. But it is still undesirable behavior for me: When I explicitly assign a type to a variable, I expect the type to not change. |
Here is a related issue: #18102 Which does point to inconsistent behaviour, for example: let num1: 5 = 5;
num1 += 7; // ok
// num1 has type number;
let num2: 5 = 5;
num2 = num2 + 7; // error I can't say if this is a bug, or by design to support certain JS idioms. Someone on the team will need to clarify that. I agree that the documentation is probably lacking on this either way though. |
This is definitely not the desired behavior. In general |
Should the operator |
@ahejlsberg You set milestone to 3.2 but I think this might be a breaking change |
Opened a pull request (a few weeks back) to fix an issue related to this. #28344 |
I'm going to close this as a duplicate of #14745 and move the conversation to here. |
This code should not be valid:
The text was updated successfully, but these errors were encountered: