27 lines
738 B
TypeScript
27 lines
738 B
TypeScript
import 'reflect-metadata'
|
|
import overload from '../util/overload.js'
|
|
|
|
const addImps = [
|
|
(x: number, y: number) => {
|
|
if (typeof x === 'number' && typeof y === 'number') return x + y
|
|
throw new TypeError('Can only add numbers')
|
|
},
|
|
(x: string, y: string) => {
|
|
if (typeof x === 'string' && typeof y === 'string') {
|
|
return 'Yay' + x + y
|
|
}
|
|
throw new TypeError('or strings')
|
|
},
|
|
(x: bigint, final?: string) => x.toString() + final
|
|
] as const
|
|
|
|
const adder = overload(addImps)
|
|
|
|
console.log(adder(1, 2))
|
|
console.log(adder('a', 'b'))
|
|
console.log(adder(2n))
|
|
|
|
// And make sure typescript complains on signatures not covered by an imp:
|
|
//@ts-expect-error
|
|
console.log(adder(2n, 4n))
|