【发布时间】:2017-09-02 23:05:00
【问题描述】:
我正在为一些 js 库编写打字稿。我需要声明可选的可调用装饰器:
@model
class User {}
@model()
class User {}
@model('User')
class User {}
我尝试使用来自lib.es6.d.ts 的ClassDecorator,但没有成功:
// 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;
当然,我可以将手动输入作为解决方法:
export function model<TFunction extends Function>(target: TFunction): TFunction | void;
export function model(name?: string):
<TFunction extends Function>(target: TFunction) => TFunction | void;
但是在这种情况下我如何重用现有的ClassDecorator 类型呢?
【问题讨论】:
标签: javascript typescript decorator typescript-typings