Exploring the use of generic classes in TypeScript has led me to a roadblock - I can't seem to generate the specific type error I'm aiming for when an incorrect argument is passed to a function. I've attempted several different approaches, but none of them seem to work when using objects as generic type arguments. The issue only arises with more basic types like strings and booleans.
class Test<T> {
func1(_other: Test<T>) : void {}
func2(_other: T) : void {}
func3<U extends Test<T>>(_other: U) : void {}
func4<U extends T>(_other: U) : void {}
}
// CRTP
class A extends Test<A> {}
class B extends Test<B> {}
// without CRTP, object type
class G {}
class X extends Test<G> {}
// without CRTP, basic type
class Y extends Test<boolean> {}
const a = new A();
const b = new B();
const x = new X();
const y = new Y();
// No type errors encountered here. Why?
a.func1(b); a.func2(b); a.func3(b); a.func4(b);
a.func1(x); a.func2(x); a.func3(x); a.func4(x);
// However, all four functions produce type errors as expected
a.func1(y); a.func2(y); a.func3(y); a.func4(y);