呼び出し可能オブジェクト
interface ReturnString {
(): string
}declare const foo: ReturnString;
const bar = foo(); // bar is inferred as a string明白な例
interface Complex {
(foo: string, bar?: number, ...others: boolean[]): number;
}interface Overloaded {
(foo: string): string
(foo: number): number
}
// example implementation
function stringOrNumber(foo: number): number;
function stringOrNumber(foo: string): string;
function stringOrNumber(foo: any): any {
if (typeof foo === 'number') {
return foo * foo;
} else if (typeof foo === 'string') {
return `hello ${foo}`;
}
}
const overloaded: Overloaded = stringOrNumber;
// example usage
const str = overloaded(''); // type of `str` is inferred as `string`
const num = overloaded(123); // type of `num` is inferred as `number`アロー構文
ニューアブル(Newable)
最終更新