Javascript 实现函数重载
因为javascript不支持函数重载,所以typescript不支持它。但是,这是一个有效的接口声明:Javascript 实现函数重载,javascript,typescript,Javascript,Typescript,因为javascript不支持函数重载,所以typescript不支持它。但是,这是一个有效的接口声明: // function overloading only in the interface interface IFoo{ test(x:string); test(x:number); } var x:IFoo; x.test(1); x.test("asdf"); 但是我如何实现这个接口呢。Typescript不允许此代码: // function overload
// function overloading only in the interface
interface IFoo{
test(x:string);
test(x:number);
}
var x:IFoo;
x.test(1);
x.test("asdf");
但是我如何实现这个接口呢。Typescript不允许此代码:
// function overloading only in the interface
interface IFoo{
test(x:string);
test(x:number);
}
class foo implements IFoo{
test(x:string){
}
test(x:number){
}
}
Typescript中的函数重载如下所示:
class foo implements IFoo {
test(x: string);
test(x: number);
test(x: any) {
if (typeof x === "string") {
//string code
} else {
//number code
}
}
}
Typescript中的函数重载如下所示:
class foo implements IFoo {
test(x: string);
test(x: number);
test(x: any) {
if (typeof x === "string") {
//string code
} else {
//number code
}
}
}
我想你已经在翻译器中发现了一个bug。将功能添加到接口的原因很清楚。这就是允许我们(通过接口)描述jquery如何工作的原因。但是我想了解的是如何在typescript中编写类似jquery的东西。我想说的是,您在翻译程序中发现了一个bug。将功能添加到接口的原因非常清楚。这就是允许我们(通过接口)描述jquery如何工作的原因。但我想了解的是如何在typescript中编写类似jquery的东西。