Skip to content

Literal types and their unions should not be mutable #28077

Closed

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

  1. 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
  2. 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
  3. KSXGitHub commented on Oct 23, 2018

    @KSXGitHub
    ContributorAuthor

    Jack Williams (@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.

  4. 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.

  5. 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.

  6. 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.

  7. KSXGitHub commented on Oct 23, 2018

    @KSXGitHub
    ContributorAuthor

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

  8. collin5 commented on Nov 21, 2018

    @collin5
    Contributor

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

  9. 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.

  10. added
    DuplicateAn existing issue was already created
    and removed
    BugA bug in TypeScript
    on Feb 1, 2019
  11. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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

    Issue actions