【发布时间】:2021-04-29 16:45:06
【问题描述】:
我希望强制 typescript 接口的某些成员在其实现中需要装饰器。
这是我的示例界面:
export interface InjectComponentDef<TComponent> {
// TODO is it possible to make this only allow properties decorated with @Input()
inputs: Partial<TComponent>, // & { @Input() [key: string]: any } my attempt to enforce decorator, invalid syntax
//...
};
我的目标是提供以下课程,我想禁止 notAnInput 属性包含在 InjectComponentDef.inputs 实现中
export class MyComponent {
public @Input() isAnInput: string;
public notAnInput: string;
}
...
let x:InputComponentDef<MyComponent>=
{
inputs: {
isAnInput:'all good',
notAnInput:'no good', // I want this to throw a compile time error because it is not decorated with @Input
}
}
有没有办法在 编译时在 typescript 中完成此任务?
【问题讨论】:
标签: typescript interface decorator