interfaceOverloaded { (foo:string):string (foo:number):number}// example implementationfunctionstringOrNumber(foo:number):number;functionstringOrNumber(foo:string):string;functionstringOrNumber(foo:any):any {if (typeof foo ==='number') {return foo * foo; } elseif (typeof foo ==='string') {return`hello ${foo}`; }}constoverloaded:Overloaded= stringOrNumber;// example usageconststr=overloaded(''); // type of `str` is inferred as `string`constnum=overloaded(123); // type of `num` is inferred as `number`
interfaceCallMeWithNewToGetString {new():string}// UsagedeclareconstFoo:CallMeWithNewToGetString;constbar=newFoo(); // bar is inferred to be of type string