class Thing extends ((function () {
return null;
})()) {
doThing() {
}
}
说extends null
是一种非常迂回的方式,但类型不检查,这很尴尬。 您必须使用像StringConstructor
这样的虚拟 ctor 类型来注释 IIFE 以满足检查器。
这是设计使然。 唯一支持的扩展 null 形式是extends null
其他任何东西,必须返回一个类型具有构造签名的值。 在这种情况下,我们将 any 作为通配符排除在外,只是因为这里比预期的用户错误更有可能。 看看是否有实际用例需要这样做会很有趣。
这是一个有趣的案例,在 TypeScript 2.0 中,我声明了一个模块 swagger-client ,如: declare module 'swagger-client';
。 但是,当我尝试使用它时:
import * as Swagger from 'swagger-client';
class Swagger extends Swagger {
pet: any;
constructor() {
super({
url: config.get('spec.petstore'),
usePromise: true
});
}
}
我收到错误Type 'any' is not a constructor function type
。
当我从索引文件导入时出现此错误,如下所示:
从 '.' 导入 {Derived, Base}
并且派生类在基类之前列在 index.ts 中:
从'./Derived'导出{Derived};
从 './Base' 导出 {Base};
我改变了顺序,它起作用了。
最有用的评论
这是一个有趣的案例,在 TypeScript 2.0 中,我声明了一个模块 swagger-client ,如:
declare module 'swagger-client';
。 但是,当我尝试使用它时:我收到错误
Type 'any' is not a constructor function type
。