sum 类型是个好东西,下面的代码是一段使用了 overloading 功能 TS,在本质上模拟了 Haskell 中处理 Either type 入参函数的定义形式。
// using overloading to implement function which handle Either type
function t(a: number) : string
function t(a: string) : boolean
function t(a: number | string) {
if (typeof a === 'number') {
return `${a}`
} else {
return a.indexOf('a') > -1
}
}
sum 类型是个好东西,下面的代码是一段使用了 overloading 功能 TS,在本质上模拟了 Haskell 中处理 Either type 入参函数的定义形式。