Skip to content

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

Closed
KSXGitHub opened this issue Oct 23, 2018 · 8 comments
Closed

Literal types and their unions should not be mutable #28077

KSXGitHub opened this issue Oct 23, 2018 · 8 comments
Assignees
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

This code should not be valid:

let a: 'a' = 'a'
a += 'x' // 'typeof a' is still 'a' 

let b: 5 = 5
b += 7 // 'typeof b' is still 5
@KSXGitHub KSXGitHub changed the title Literal types and and their unions should not be mutable Literal types and their unions should not be mutable Oct 23, 2018
@jack-williams
Copy link
Collaborator

jack-williams commented 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?

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

@KSXGitHub
Copy link
Contributor Author

@jack-williams

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.

@jack-williams
Copy link
Collaborator

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.

@ahejlsberg
Copy link
Member

This is definitely not the desired behavior. In general x += 1 should checked the same way as x = x + 1, and that is not the case here.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Oct 23, 2018
@ahejlsberg ahejlsberg added this to the TypeScript 3.2 milestone Oct 23, 2018
@jack-williams
Copy link
Collaborator

Should the operator ++ be treated in the same way? That also widens the literal type.

@KSXGitHub
Copy link
Contributor Author

@ahejlsberg You set milestone to 3.2 but I think this might be a breaking change

@collin5
Copy link
Contributor

collin5 commented Nov 21, 2018

Opened a pull request (a few weeks back) to fix an issue related to this. #28344

@ahejlsberg
Copy link
Member

I'm going to close this as a duplicate of #14745 and move the conversation to here.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created and removed Bug A bug in TypeScript Help Wanted You can do this labels Feb 1, 2019
@ahejlsberg ahejlsberg removed this from the TypeScript 3.4.0 milestone Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants