Skip to content

Literal types and their unions should not be mutable #28077

Closed
@KSXGitHub

Description

@KSXGitHub

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

Activity

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

jack-williams commented on Oct 23, 2018

@jack-williams
Collaborator

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

KSXGitHub commented on Oct 23, 2018

@KSXGitHub
ContributorAuthor

@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

jack-williams commented on Oct 23, 2018

@jack-williams
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

ahejlsberg commented on Oct 23, 2018

@ahejlsberg
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.

added this to the TypeScript 3.2 milestone on Oct 23, 2018
jack-williams

jack-williams commented on Oct 23, 2018

@jack-williams
Collaborator

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

KSXGitHub

KSXGitHub commented on Oct 23, 2018

@KSXGitHub
ContributorAuthor

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

collin5

collin5 commented on Nov 21, 2018

@collin5
Contributor

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

ahejlsberg

ahejlsberg commented on Feb 1, 2019

@ahejlsberg
Member

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

added
DuplicateAn existing issue was already created
and removed
BugA bug in TypeScript
on Feb 1, 2019
removed this from the TypeScript 3.4.0 milestone on Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

DuplicateAn existing issue was already created

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @DanielRosenwasser@ahejlsberg@jack-williams@KSXGitHub@collin5

      Issue actions

        Literal types and their unions should not be mutable · Issue #28077 · microsoft/TypeScript