简体   繁体   English

TypeScript中的函数重载

[英]Function Overloading in TypeScript

Is function overloading not supported in TypeScript? 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. 在vs代码中编译时,出现以下错误:提供的参数与调用目标的任何签名都不匹配。 Duplicate function implementation. 功能实现重复。

Overloading in typescript is done by using optional parameters . 使用可选参数可以完成typescript中的重载。

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 缺点是在同一功能中有逻辑。您可以检查问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM