Skip to content

error TS2365: Operator '+' cannot be applied to types 'string | number' and 'string | number'. #49661

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
borisovg opened this issue Jun 23, 2022 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@borisovg
Copy link

borisovg commented Jun 23, 2022

Bug Report

Code does not compile.

🔎 Search Terms

"Operator '+' cannot be applied to types".

🕗 Version & Regression Information

4.7.4

💻 Code

const foo = { foo: 123, bar: 'bar' };

for (const k of ['foo', 'bar'] as const) {
  const val = foo[k];
  console.log(val + val);
}

🙁 Actual behavior

src/test.ts:5:15 - error TS2365: Operator '+' cannot be applied to types 'string | number' and 'string | number'.

🙂 Expected behavior

No error.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 23, 2022
@RyanCavanaugh
Copy link
Member

Operations are evaluated according to the types of their operands, not their identities. The left-hand type is string | number, the right-hand type is string | number, and the rules for + disallow an ambiguous operation like that. Whether or not the LHS and RHS refer to the same variable (or what "same" would even mean apart from bare identifiers) isn't an input to the algorithm, nor is it clear how that could be generalized.

@kelvinsekx
Copy link

Experienced something similar.

function universalAdd(monoid1: string|number){
  return monoid1 + monoid1;
}

It took me sometime to understand your statement "types of their operands, not their identities". But I got it thanks @RyanCavanaugh

@kelvinsekx
Copy link

can we close this issue already?

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
@haptn
Copy link

haptn commented Jul 14, 2023

Operations are evaluated according to the types of their operands, not their identities. The left-hand type is string | number, the right-hand type is string | number, and the rules for + disallow an ambiguous operation like that. Whether or not the LHS and RHS refer to the same variable (or what "same" would even mean apart from bare identifiers) isn't an input to the algorithm, nor is it clear how that could be generalized.

I'm sorry but I'm still not clear on the problem here. If I replace the union type of string | number to type any, things will work normally.
So what I'm confusing here is that does the + operation only accepts inputs with single types (number, string, any), but not union type (string | number)?

Thank you!

@nishat-md-r-rafi
Copy link

If you are sure about the type by type guard operator "typeof". and then assume the type using "as"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants