88

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

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

and

public emit<T1,T2>(event: string, arg1: T1, arg2: T2): void {}

However when transpiling the typescript I get the following error

error TS2393: Duplicate function implementation.

I thought you could overload functions in typescript providing the number of parameters in the function signature were different. Given that the above signatures have 2 and 3 parameters respectively, why am I getting this transpilation error?

6
  • 2
    There is no function overloading in typescript, not even without generics. Commented Sep 25, 2016 at 17:44
  • 11
    Please read the documentation on overloading more closely. Overloading does not mean you can provide multiple implementations; it means you you can provide multiple signatures, with a single implementation. But in this case why are you not simply writing arg2?? Commented Sep 25, 2016 at 17:46
  • @torazaburo. I'm trying to ensure type safety through generics. If I use arg2? I'll still have to provide the generic type T2 in emit<T1,T2> even though I may not actually be using T2. I guess I'm trying to achieve something similar to c#'s Func and Action delegate signatures. But there could be a better way. Commented Sep 25, 2016 at 17:54
  • 2
    If you are referring to providing the generic type T2 in the definition of the function, this harms nothing. If are referring to calling the function, providing any type, as in emit<number, string> shouldn't be necessary since types will be picked up from the types of the arguments. Anyway, if necessary, write out the two declarations with no body (just a semi-colon), then write one implementation that somehow checks for the presence of arg2, or perhaps assigns it a default value. Commented Sep 25, 2016 at 18:03
  • Does this answer your question? TypeScript function overloading Commented Jul 22, 2022 at 10:49

3 Answers 3

70

I'm assuming your code looks like this:

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
}

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<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
}
Sign up to request clarification or add additional context in comments.

4 Comments

When you overload in TypeScript, you only have one implementation with multiple signatures. See stackoverflow.com/questions/13212625/…
So if that three function contains different logics?
There are three signatures, but just one implementation. If you need different logic depending on what signature was used, you'll need to add some extra checks inside your implementation. For example, you could check args.length to figure out if only arg1 was passed, or if both arg1 and arg2 were passed.
These are all void functions. But what to do with generic return values. For example, what if the return type of the first overload is T1 (or string) while the second overload return type if T2?
26

Because you open both Typescript and JavaScript file in the same window in IDE you can solve this problem with three ways:

First Solution:

Open typescript file only or JavaScript file only

Second solution - run this command:

tsc --init

This command will create tsconfig.json file for typescript configuration

Last Solution write

export{}

In the top of the file

Comments

16
export {}

Just add this line at the top of the typescript file

3 Comments

It works. The question ist: Why? :-)
"Another cause of the error is having a glitch due to legacy script files. If you only have a single definition for the function in the file, add the export {} line to your file to make it an ES Module" From this blog
still getting error, Uncaught ReferenceError: exports is not defined at app.js:2:23 above error is after compilation of typescript and conversion to javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.