Rich
Rich

Reputation: 6561

Function Overloading in TypeScript

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.

Upvotes: 0

Views: 719

Answers (1)

Suraj Rao
Suraj Rao

Reputation: 29614

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

Upvotes: 2

Related Questions