Closed
Description
TypeScript Version: 4.1.0-dev.20200917
Search Terms:
generics, generic constraints, extends, intersection, intersect, assignable, type safety, compatible
Code
class Scene<X> {
constructor(public x: X){}
something(): number {return 0}
}
interface A {
a: string
}
interface B {
b: string
}
class ABScene<X extends A & B> extends Scene<X> {
something(){
return this.x.a.length + this.x.b.length;
}
}
const a: Scene<A> = new ABScene<A & B>({a: 'a', b: 'b'}); // no error?
// This circumvents the constraint that X must extend A & B.
a.x = {a: 'a'}
// We can now assign just A.
a.something()
// The method fails because it tries to access b which is not defined anymore.
Expected behavior:
There should be an error that type 'A' does not satisfy the constraint 'A & B'.
Actual behavior:
It compiles with no error.
I guess the compiler forgets the generic constraint of the subclass.
Playground Link:
Related Issues:
#31006