typocomath/src/numbers/type.ts

27 lines
1.1 KiB
TypeScript

import {ImpType} from '../core/Dispatcher.js'
export const number_type = {
before: ['Complex'],
test: (n: unknown): n is number => typeof n === 'number',
from: {string: s => +s}
}
export interface NumbersReturn<Params> {
// The following description of the return type of `zero` on a single
// number argument has ended up unfortunately rather complicated. However,
// it illustrates the typing is really working: Suppose we have a
// `type Small = 1 | 2 | 3`. Then Small indeed extends number, but we
// can't use the operation `zero(s: Small)` because zero is supposed to
// return something of the same type as its argument, but there is no
// zero in Small. Anyhow, in plain language the below says that given
// one parameter of a subtype of number, as long as that subtype includes 0,
// the zero operation returns a member of the type `0` (so we know even
// at compile time that its value will be 0).
zero: Params extends [infer T]
? T extends number ? 0 extends T ? 0 : never : never
: never
}
export const zero: ImpType<'zero', [number]> = a => 0