Maximus Maximus - 15 days ago 9
TypeScript Question

Class decorator type mismatch error

The

ClassDecorator
is defined as:

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


I've written it like that:

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


But the compiler gives me an error:

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


Why?

Answer

The type ClassDecorator is defined to be a function that takes one argument, you return a function that takes three arguments, that is not compatible and that is why you get the error message.