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
class BaseClass {
func<K extends keyof this>(k: K) {}
}
class A extends BaseClass {
a = 1;
b = 2;
}
class B extends BaseClass {
b = 3;
c = 4;
}
function func<T, K extends keyof T>(v: T, k: K) {}
let a = new A();
let ab: A | B = new A() as any;
func(a, 'a');
func(a, 'b');
// func(a, 'c'); // compile error
// func(ab, 'a'); // compile error
func(ab, 'b');
a.func('a');
a.func('b');
// a.func('c'); // compile error
// ab.func('a'); // compile error
ab.func('b'); // compile error NOT EXPECTED
π Actual behavior
Compiler Errors
This expression is not callable.
Each member of the union type '((k: K) => void) | ((k: K) => void)' has signatures, but none of those signatures are compatible with each other.
π Expected behavior
last line should not raise Compiler Errors, (<K extends keyof A>(k: K) => void) | (<K extends keyof B>(k: K) => void) should be union to (<K extends keyof A|B>(k: K) => void) ?
Also notice that global function "func" can work as expected.
The text was updated successfully, but these errors were encountered:
last line should not raise Compiler Errors, ((k: K) => void) | ((k: K) => void) should be union to (<K extends keyof A|B>(k: K) => void) ?
In general this is not a safe reduction step: contravariant uses of K would become unsound. It's very rarely possible to safely merge two arbitrary signatures.
Bug Report
Calling generic function declared at base class with union type cause unexpected compile error.
π Search Terms
Union generic, keyof this
π Version & Regression Information
TypeScript version: 4.7.4
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Compiler Errors
This expression is not callable.
Each member of the union type '((k: K) => void) | ((k: K) => void)' has signatures, but none of those signatures are compatible with each other.
π Expected behavior
last line should not raise Compiler Errors,
(<K extends keyof A>(k: K) => void) | (<K extends keyof B>(k: K) => void)
should be union to (<K extends keyof A|B>(k: K) => void) ?Also notice that global function "func" can work as expected.
The text was updated successfully, but these errors were encountered: