-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic types combined with & instead of | #48287
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
Comments
This is a correct error. Here is a simple example in which your expected behavior is unsound: class ExampleClass<T> {
test(value: T): T {
return value;
}
}
class Example1 extends ExampleClass<string> {
test(value: string): string {
return value.toLowerCase();
}
}
class Example2 extends ExampleClass<number> {
test(value: number): number {
return Number(value.toFixed(2));
}
}
function Example(example: 1|2) // returns `typeof Example1 | typeof Example2`
{
return Math.random() > 0.5 ? Example1 : Example2;
}
const instance = new (Example(1))();
console.log(instance.test('Foo'))
// You expect it return 'foo', but there's a 50% chance of runtime error See: #30769 for more details. |
I don't see how this would be correct. |
Yes, because the first |
Thanks @fatcerberus, I understand now |
Uh oh!
There was an error while loading. Please reload this page.
🔎 Search Terms
generic, type, class, and, &, or, |, combined
⏯ Playground Link
Playground
💻 Code
🙁 Actual behavior
Combining classes with generic type results in generic types on arguments to be combined with
&
.But get combined with
|
for return types.🙂 Expected behavior
Expected the types to be combined with
|
The text was updated successfully, but these errors were encountered: