首页 > 解决方案 > TypeScript 泛型:不满足约束

问题描述

我正在尝试制作打字稿以警告错误的类被解释为泛型类型参数。

预期结果:

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

标签: typescriptoopgenericsconstructortype-parameter

解决方案


推荐阅读