-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Operator '+' cannot be applied to types 'T' and 'T'. #12410
Comments
Also doesn't work correctly for simple union type:
|
For those who want to tackle this, you might need to use |
Am I right? |
@mhegazy I noticed that your comment in #13073 said this should be relaxed for
|
looking at this again, i do not think this is as simple as i originally thought. we need to talk about the new rules for the operator. |
@arusakov To expand that out: x + y; // resultType is `string | number`
x + s; // resultType is `string`
x + n; // resultType is `string | number` that looks correct. However, if |
what about |
I think it should be |
You don't know specifically whether each was a |
Accepting PRs for the rules:
|
@RyanCavanaugh surely if For example in my example I know that whatever I use as the generic type |
@Jameskmonger if It really sounds like you should use overloads here rather than a type parameter. |
Ah of course @RyanCavanaugh - I was misunderstanding how generic types work with union types. Thanks for explaining 😄 |
Can this be closed? I think the rules @RyanCavanaugh gave are currently satisfied: function addString<T extends string>(x: T, y: T): string {
return x + y;
}
function addNum<T extends number>(x: T, y: T): number {
let a = x * y;
let b = x - y;
let z = x++;
return x + y;
}
addString("a", "b");
addNum(1, 2); |
thanks! |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
Code to compile fine, because
T
is of typestring | number
and therefore can have the+
operator applied.Actual behavior:
Operator '+' cannot be applied to types 'T' and 'T'.
The text was updated successfully, but these errors were encountered: