Aby zaimplementować funkcję przeciążoną, napisz najpierw wszystkie sygnatury połączeń przeciążeniowych, które mają być widoczne, a następnie podpis implementacji stanowiący nadzbiór nad wszystkimi sygnaturami przeciążenia. Przykład:
class Thing implements IThing {
// Implement the name and age fields
name: string;
age: number;
// Overload signature #1
sayHello(name: string): string;
// Overload signature #2
sayHello(age: number): number;
// Implementation signature, not visible to external callers
sayHello(arg: any): any {
if(typeof arg === 'string') {
return this.name;
} else if(typeof arg === 'number') {
return this.age;
} else {
throw new Error('sayHello can only take string or a number');
}
}
}
var x = new Thing();
var n = x.sayHello('world'); // n: string
var a = x.sayHello(42); // a: number
var e = x.sayHello(false); // error: false is not string or number
To nie wygląda na prawidłowy TypeScript. Czy obejrzałeś tutaj przykłady kodu? http://www.typescriptlang.org/Tutorial/ –
Nie ma błędów za pośrednictwem IntelSense, otrzymuję ten fragment kodu z filmu wprowadzającego maszynopis tutaj: http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing -TypeScript –
To jest całkowicie poprawny TypeScript i jest to poprawne, proste pytanie. Głosuj, aby ponownie otworzyć. –