如何使用 Typescript 中的装饰器向类添加静态属性

bes*_*ant 6 decorator typescript typescript-typings

我正在尝试使用装饰器向类添加一些属性。但我收到编译时错误。我的代码结构有点像这样。

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() {

    }
}
Run Code Online (Sandbox Code Playgroud)

相当长的有问题的代码,但我遇到了以下错误,我无法找出我的装饰器或类型有什么问题。

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'.
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗???