TypeScript泛型:不满足约束

时间:2019-12-19 16:46:40

标签: typescript oop generics constructor type-parameter

我正在尝试使打字稿发出警告,以将错误的类解释为通用类型参数。

预期结果:

class A { }

class B extends A { }

class C<T extends A> { }

const instance1 = new C<A>(); // error: wrong type
const instance2 = new C<B>(); // success: inherited type

实际结果:

class A { }

class B extends A { }

class C<T extends A> { }

const instance1 = new C<A>(); // success: but the type is wrong
const instance2 = new C<B>(); // success: inherited type

0 个答案:

没有答案