我在同一个Typescript类中定义了以下两个函数签名,即,
public emit<T1>(event: string, arg1: T1): void {}和
public emit<T1,T2>(event: string, arg1: T1, arg2: T2): void {}但是,在转换typescript时,我得到了以下错误
error TS2393: Duplicate function implementation.我认为你可以重载typescript中的函数,前提是函数签名中的参数数量不同。假设上面的签名分别有2个和3个参数,为什么我会得到这个转写错误?
发布于 2016-09-26 02:15:32
我假设你的代码是这样的:
public emit<T1>(event: string, arg1: T1): void {}
public emit<T1,T2>(event: string, arg1: T1, arg2: T2): void {}
public emit(event: string, ...args: any[]): void {
// actual implementation here
}问题是在前两行之后有{}。这实际上定义了一个函数的空实现,例如:
function empty() {}您只想为函数定义一个类型,而不是一个实现。因此,仅用分号替换空块:
public emit<T1>(event: string, arg1: T1): void;
public emit<T1,T2>(event: string, arg1: T1, arg2: T2): void;
public emit(event: string, ...args: any[]): void {
// actual implementation here
}发布于 2021-10-27 14:54:14
export {}只需将此行添加到typescript文件的顶部
https://stackoverflow.com/questions/39689763
复制相似问题