typomath/src/steps/five.ts

27 lines
697 B
TypeScript

import 'reflect-metadata'
import {reflect} from 'typescript-rtti'
import {merge, overloadValues} from '../util/overload.js'
const numImps = {
add: [(x: number, y: number) => x + y],
negate: [(x: number) => -x]
} as const
const strImps = {
add: [(x: string, y: string) => x + ', ' + y],
negate: [(x: string) => 'NOT ' + x]
} as const
const merger = merge(numImps)
const mathImps = merger.with(strImps).imps()
const math = overloadValues(mathImps)
console.log(math.add(1.5, 2.5))
console.log(math.add('One and a half', 'Two and a half'))
console.log(math.negate(3.5))
console.log(math.negate('Three and a half'))
//@ts-expect-error
console.log(math.add(1.5, 'Two and a half'))