2016-11-21 153 views
0

ClassDecorator被定义为:类装饰类型不匹配错误

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void; 

我都写过这样的:

export function ClassDecorator(params: any): ClassDecorator { 
    return function (target) { 
     Object.seal(target); 
     Object.seal(target.prototype); 
    } 
} 

但是,编译器给我一个错误:

Error:(2, 12) TS2322:Type '(target: any, key: any, descriptor: any) => void' is not assignable to type 'ClassDecorator'. 

为什么?

回答

1

ClassDecorator类型被定义为一个函数,它接受一个参数,您返回一个带有三个参数的函数,这是不兼容的,这就是为什么您会收到错误消息。

+0

谢谢,一旦我修复,我得到另一个错误:'错误:(4,22)TS2339:属性'原型'不存在类型'对象'。' –

+0

@it应该工作,如果你说目标是'任何',它看起来像你在代码中隐含的。你说它应该是'Object'类型吗? – Alex

+0

它应该是'Function'或'TFunction'类型,据我所知,类型定义 –