2
我有这种打字稿的代码,使用泛型打字工作在2.3以下,但现在打破在更严格的打字强制打字稿2.4.1。打字稿2.4.1和更严格的泛型打破typeof /继承
我写了这个最小的代码片段来演示这个问题:
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)那曾经工作,现在打破了(如果我添加“noStrictGenericChecks”:真对我的tsconfig,当然它编译)。
如果我删除private c: T;
部分,它也编译。请注意,在我的实际代码中,这个类成员确实存在,但是我扩展的类来自外部库,所以我无法删除它,此外,我希望它可以用它进行编译。
有什么办法可以让这段代码编译并保留字符串的输入吗?
这是一个非常聪明的解决方案,它完美的工作!非常感谢! – jbar