【发布时间】:2017-07-30 17:06:07
【问题描述】:
我有这个打字稿代码,它使用了过去在打字稿 2.3 下工作的泛型,但现在在打字稿 2.4.1 更严格的打字强制下打破了。
我写了这个最小的代码 sn-p 来演示这个问题:
class A {}
class B extends A {}
function helloA(clazz: typeof A) {}
helloA(B); // fine
class C<T> {
private c: T;
};
class D extends C<string> {}
function helloC(clazz: typeof C) {}
helloC(D); // breaks
来自 tsc 2.4.1 的错误如下:
test.ts(11,8): error TS2345: Argument of type 'typeof D' is not assignable to parameter of type 'typeof C'.
Type 'D' is not assignable to type 'C<T>'.
Types of property 'c' are incompatible.
Type 'string' is not assignable to type 'T'.
所以 helloA(B) 可以正常工作,而以前可以正常工作的 helloC(D) 现在会中断(如果我在我的 tsconfig 中添加 "noStrictGenericChecks": true ,它当然可以编译)。
如果我删除 private c: T; 部分,它也会编译。请注意,在我的实际代码中,这个类成员确实存在,但我扩展的类是来自外部库,所以我不能删除它,此外,我希望它与它一起编译。
有没有办法让这段代码编译并保留字符串类型?
【问题讨论】:
标签: typescript generics typeof