有办法为ES6混合编写类型文本定义吗?
我在library.js中有这个模式,我想创建library.d.ts
// declaration in `library.js`
class Super extends Simple {
constructor() {}
static Compose(Base = Super) {
return class extends Base {
// ...
}
}
}
// usage in `client.js`
class MyClass extends Super.Compose() {}
let myInstance = new MyClass();
class MyOtherClass extends Super.Compose(AnotherClass) {}发布于 2016-09-11 06:51:14
不,类型类型系统没有足够的表达能力--参见https://github.com/Microsoft/TypeScript/issues/7225和https://github.com/Microsoft/TypeScript/issues/4890中的讨论。
打字本中的习语“类”写成
interface Constructor<T> {
new (...args): T;
}因此,撰写声明的一种方法是
export declare class Simple {}
export declare class Super extends Simple {
static Compose<T>(Base?: Constructor<T>): Constructor<T & {/*mixed-in declarations*/}>
}也就是说,复合返回类型被声明为交集类型的构造函数--这种类型必须具有参数(Base)的所有属性以及mixin的所有属性。
您可以像这样使用这个声明(假设它在library.d.ts文件中)
import {Super} from './library'
let MyComposed = Super.Compose(Super)
let myInstance = new MyComposed不方便之处在于,您总是必须为Super.Compose()提供参数,因为类型推断在不知道默认参数的值的情况下无法工作,并且不能在声明文件中为默认参数提供值。
但最大的问题是你不能把作文的结果作为一个类来使用:
class MyClass extends Super.Compose(Super) {}由于上述问题而不进行汇编:
error TS2509: Base constructor return type 'Super & {}' is not a class or interface type.https://stackoverflow.com/questions/39430443
复制相似问题