(+1 overload)
function foo(x: string): string
function foo(x: number, y: number): number
function foo(x?, y?): any {
if (typeof y !== 'undefined') {
return x + y
}
return x
}
foo('hello')
foo(1, 2)
注意:
前两行为函数声明(函数签名)
第三行是函数实现,参数必须全部为<?>可选类型
类似的:
constructor (+1 overload)
class Store {
constructor(x: string)
constructor(x: number, y: number)
constructor(x?, y?) {}
}
new Store('hello')
new Store(1, 2)