Skip to content

No error report when using compound assignment operator with literal types. #30783

Closed

Description

TypeScript Version: 3.4.2

  • literal type shorthand assignment
  • shorthand assignment

Code

let ten = 10 as const;

// no error reports
ten += 2;
ten -= 2;
ten *= 2;
ten /= 2;
ten **= 2;
ten %= 2;
ten |= 2;
ten &= 2;
ten ^= 2;
ten >>= 2;
ten >>>= 2;
ten <<= 2;

// all errors as expected
ten = ten + 2;
ten = ten - 2;
ten = ten * 2;
ten = ten / 2;
ten = ten ** 2;
ten = ten % 2;
ten = ten | 2;
ten = ten & 2;
ten = ten ^ 2;
ten = ten >> 2;
ten = ten >>> 2;
ten = ten << 2;

let pinkie = 'Pinkie' as const;

// no error report
pinkie += 'Pie';

// error as expected
pinkie = pinkie + 'Pie';

Expected behavior:

Shorthand assignment to a literal type where the resulting assigned value may not always be of that literal type should generate a compile-time error likewise its expanded form.

Actual behavior:
There is no error report for using shorthand assignment operators that might store an invalid value for that literal type. But the expanded form of those operators works as expected.

Playground Link:

Klick me

Related Issues:

Activity

  1. Veetaha commented on Apr 5, 2019

    @Veetaha
    Author

    Quick inline proposal: compute literal types expressions at compile-time so that ten * 2 would be of type 20 but not number or type of 'Pinkie' + 'Pie' expression would be 'PinkiePie' instead of string.

  2. changed the title [-]No error report when using shorthand assignment operator with literal types.[/-] [+]No error report when using compound assignment operator with literal types.[/+] on Apr 6, 2019
  3. DanielRosenwasser commented on Apr 6, 2019

    @DanielRosenwasser
    Member

    Potentially related to #13865, but I feel like we have another issue about this.

  4. RyanCavanaugh commented on Apr 9, 2019

    @RyanCavanaugh
    Member

    Duplicate #14745

  5. typescript-bot commented on Apr 12, 2019

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

Assignees

No one assigned

    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