Take a look at this piece of code:
class A<T> { t?: T; }
interface B {}
class C implements A<B> {}
function f<T1 extends A<T2>, T2>(a: T1): T2 | undefined { return a.t; }
const result = f(new C());
const result2 = f(new A<B>());
Interestingly, the type of result
and even result2
will be unknown
, despite being inferable that it should be B
based on context since C
is implementing A<B>
.
Why doesn't TypeScript automatically infer this? Is it due to a missing feature, an unsound inference, or is there an alternative way to achieve the desired behavior?