javascript - Implementing function overloading -
since javascript not support function overloading typescript not support it. valid interface declaration:
// function overloading in interface interface ifoo{ test(x:string); test(x:number); } var x:ifoo; x.test(1); x.test("asdf");
but how can implement interface. typescript not allow code:
// function overloading in interface interface ifoo{ test(x:string); test(x:number); } class foo implements ifoo{ test(x:string){ } test(x:number){ } }
function overloading in typescript done this:
class foo implements ifoo { test(x: string); test(x: number); test(x: any) { if (typeof x === "string") { //string code } else { //number code } } }
Comments
Post a Comment