0

Is function overloading not supported in TypeScript?

I have these two functions:

checkCredits() {
   // my code
}

checkCredits(header: any) {
    // my code
}

And I call the second function like this:

this.checkCredits(this.myObject); 

When compiling in vs code I get these errors: Supplied Parameters do not match any signature of call target. Duplicate function implementation.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Rich
  • 6,470
  • 15
  • 32
  • 53

1 Answers1

2

Overloading in typescript is done by using optional parameters.

checkCredits(header?: any) {
    // my code
}

Now you can call:

this.checkCredits(this.myObject); 

and

this.checkCredits(); 

Downside you have the logic in the same function.You can check the issue

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Thanks! I wasn't aware of that. – Rich Mar 06 '17 at 13:56
  • 1
    @Rich you can also do it [this](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Functions.md#overloads) way. But didnt seem necessary in your example – Suraj Rao Mar 06 '17 at 13:57