Skip to content

addition assignment behaves surprisingly with union types #17204

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
rkirov opened this issue Jul 14, 2017 · 1 comment
Closed

addition assignment behaves surprisingly with union types #17204

rkirov opened this issue Jul 14, 2017 · 1 comment
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Milestone

Comments

@rkirov
Copy link
Contributor

rkirov commented Jul 14, 2017

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

let x: string | number = 0;
x += ''; // ERROR(2,1): Type 'string' is not assignable to type 'number'.

Expected behavior:
Since this is ok

let x: string | number = 0;
x = x + ''; 

similarly (showing that this has to do with type-narrowing)

function f(x: string | number) {
  x += '';
}

and finally TS allows string and number additions, thus I expect the original to also be accepted by the compiler.

Generally treating x = x + 1 and x += 1 differently from type checking perspective is surprising, but maybe this is byproduct of other designed behaviors.

Actual behavior:
Error - Type 'string' is not assignable to type 'number'.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 14, 2017
@mhegazy mhegazy added this to the Future milestone Jul 20, 2018
@RyanCavanaugh RyanCavanaugh added the Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it label Dec 16, 2021
@RyanCavanaugh
Copy link
Member

I'm not really sure if this is a bug per se or not, but it doesn't seem like something that needs time spent on it either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

3 participants