You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]Literal types and and their unions should not be mutable[/-][+]Literal types and their unions should not be mutable[/+]on Oct 23, 2018
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?
leta: 'a'='a';a+='x';constx: 'a'=a;// string not assignable to 'a'letb: 5=5b+=7;consty: 5=b;// number is not assignable to 5
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
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.
Which does point to inconsistent behaviour, for example:
letnum1: 5=5;num1+=7;// ok// num1 has type number;letnum2: 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.
Activity
[-]Literal types and and their unions should not be mutable[/-][+]Literal types and their unions should not be mutable[/+]jack-williams commentedon Oct 23, 2018
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?
KSXGitHub commentedon Oct 23, 2018
@jack-williams
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.
jack-williams commentedon Oct 23, 2018
Here is a related issue: #18102
Which does point to inconsistent behaviour, for example:
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.
ahejlsberg commentedon Oct 23, 2018
This is definitely not the desired behavior. In general
x += 1
should checked the same way asx = x + 1
, and that is not the case here.jack-williams commentedon Oct 23, 2018
Should the operator
++
be treated in the same way? That also widens the literal type.KSXGitHub commentedon Oct 23, 2018
@ahejlsberg You set milestone to 3.2 but I think this might be a breaking change
collin5 commentedon Nov 21, 2018
Opened a pull request (a few weeks back) to fix an issue related to this. #28344
ahejlsberg commentedon Feb 1, 2019
I'm going to close this as a duplicate of #14745 and move the conversation to here.