【发布时间】:2019-01-16 15:20:55
【问题描述】:
我正试图围绕接口中的泛型类型,并将一个接口属性的数据传递给另一个。
举个例子可能更容易理解:
interface IExample {
model: () => Record<string, any>
evaluator: Record<string, () => boolean>
}
用例是这样的:
const example: IExample = {
model: () => {
// The content can be changed by the user
return {
k1: true,
k2: 'some content',
k3: [1, 2, 3]
}
},
evaluator: {
testEvaluator: function () {
/**
* This is where I would like to have an autocompletion
* So internally example.model() is called and
* the return value is passed as `this`-argument
*/
return this.k1 === true
}
}
}
如代码注释中所述,我想提供自动完成功能 当用户为评估者编写代码时向用户展示
到目前为止,我尝试过的大致是这样的:
interface IExample<Model extends Record<string, any> = Record<string, any>> {
model: () => Data,
evaluator: <Record<string, (this: Data) => boolean>
}
这甚至可能吗?如果是这样:我真的很感激任何提示。
【问题讨论】:
标签: typescript generics interface