Skip to content

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

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
Veetaha opened this issue Apr 5, 2019 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@Veetaha
Copy link

Veetaha commented Apr 5, 2019

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:

@Veetaha
Copy link
Author

Veetaha commented Apr 5, 2019

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.

@DanielRosenwasser DanielRosenwasser 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. Apr 6, 2019
@DanielRosenwasser
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 9, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #14745

@typescript-bot
Copy link
Collaborator

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

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

4 participants