You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
error TS2365: Operator '+' cannot be applied to types 'string | number' and 'string | number'.聽#49661
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.
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)?
Activity
RyanCavanaugh commentedon Jun 23, 2022
Operations are evaluated according to the types of their operands, not their identities. The left-hand type is
string | number, the right-hand type isstring | 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 commentedon Mar 23, 2023
Experienced something similar.
It took me sometime to understand your statement "types of their operands, not their identities". But I got it thanks Ryan Cavanaugh (@RyanCavanaugh)
kelvinsekx commentedon Mar 23, 2023
can we close this issue already?
haptn commentedon Jul 14, 2023
I'm sorry but I'm still not clear on the problem here. If I replace the union type of
string | numberto typeany, 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 commentedon Nov 8, 2023
If you are sure about the type by type guard operator "typeof". and then assume the type using "as"