Typescript Duplicate Function Implementation

前端 未结 1 912
孤街浪徒
孤街浪徒 2021-01-01 08:21

I have defined the following two function signatures in the same Typescript class, i.e.,

public emit(event: string, arg1: T1): void {}
         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 09:20

    I'm assuming your code looks like this:

    public emit(event: string, arg1: T1): void {}
    public emit(event: string, arg1: T1, arg2: T2): void {}
    public emit(event: string, ...args: any[]): void {
        // actual implementation here
    }
    

    The problem is that you have {} after the first 2 lines. This actually defines an empty implementation of a function, i.e. something like:

    function empty() {}
    

    You only want to define a type for the function, not an implementation. So replace the empty blocks with just a semi-colon:

    public emit(event: string, arg1: T1): void;
    public emit(event: string, arg1: T1, arg2: T2): void;
    public emit(event: string, ...args: any[]): void {
        // actual implementation here
    }
    

    0 讨论(0)
提交回复
热议问题