Currently, I am in the process of creating TypeScript typings for a JavaScript library. One specific requirement is to define an optional callable decorator:
@model
class User {}
@model()
class User {}
@model('User')
class User {}
I attempted to utilize the ClassDecorator
from lib.es6.d.ts
, but unfortunately, it was unsuccessful:
// works
export const model: ClassDecorator;
// error TS1238: Unable to resolve signature of class decorator when called as an expression. Cannot invoke an expression whose type lacks a call signature. Type 'ClassDecorator | CallableModelDecorator' has no compatible call signatures
type CallableModelDecorator = (name?: string) => ClassDecorator;
export const model: ClassDecorator | CallableModelDecorator;
As a temporary solution, I could manually create the typing:
export function model<TFunction extends Function>(target: TFunction): TFunction | void;
export function model(name?: string):
<TFunction extends Function>(target: TFunction) => TFunction | void;
However, I am curious about how I can reuse the existing ClassDecorator
type in this scenario.