Javascript 在Typescript中键入可选的可调用装饰器

Javascript 在Typescript中键入可选的可调用装饰器,javascript,typescript,decorator,typescript-typings,Javascript,Typescript,Decorator,Typescript Typings,我正在为一些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 d

我正在为一些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;
导出函数模型(目标:TFunction):TFunction | void;
导出函数模型(名称?:字符串):
(目标:TFunction)=>TFunction | void;

但是在这种情况下,如何重用现有的
ClassDecorator
类型?

问题是您使用的是联合类型,这些类型的变量只有两种类型的公共成员,因此在这种情况下,由于只有一种类型是可调用的,因此联合将不可调用

您正在寻找一个交叉点类型,它将具有两种类型的成员

export const model: ClassDecorator & CallableModelDecorator;

它起作用了!谢谢你的解释。文件