我正在尝试使用装饰器向类中添加一些属性。但我得到编译时间错误。 我的代码结构有点像这样。
type ModuleConfig = {
ModuleName: string
// ...other properties
}
type ModuleClass = {
ModuleName: string
}
type TargetClass = {}
function Module(moduleConfig: ModuleConfig ): Function {
return function(targetClass: TargetClass): ModuleClass {
Object.defineProperty(targetClass,"ModuleName", { value: moduleConfig.ModuleName })
// ...Define other properties in targetClass based on other properties of moduleConfig.
return <ModuleClass>targetClass
}
}
==================================
@Module({
ModuleName: "App"
// ...other properties
})
class AppModule {}
===================================
let Config = {
RootModule: AppModule,
modules:[
// other Modules
]
}
===================================
type ModulesConfig = {
RootModule: ModuleClass,
modules?: Array<ModuleClass>
}
function RegisterModules(config: ModulesConfig): Function {
return function(target: any, name: string, descriptor: PropertyDescriptor): PropertyDescriptor {
let origFn = descriptor.value
let newFn = function() {
let RootModule = config.RootModule.ModuleName
let args: FinalConfig = {
RootModule: RootModule,
modules: {config.RootModule, ...config.modules}
}
origFn.call(target, args)
}
descriptor.value = newFn
return descriptor
}
class ThatTakesCareofSomeCoreFunctionality {
@RegisterModules(Config)
intialize() {
}
}
相当长的问题代码,但我收到以下错误, 我无法找出装饰者或打字机的错误。
Argument of type '{ RootModule: typeof AppModule; }' is not assignable to parameter of type 'ModulesConfig'.
Types of property 'RootModule' are incompatible.
Type 'typeof AppModule' is not assignable to type 'ModuleClass'.
Property 'ModuleName' is missing in type 'typeof AppModule'.
我错过了什么吗?