feat: Implement step one of the roadmap (#1)
Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: #1
This commit is contained in:
parent
4db2d968e4
commit
0430eb6d5c
5 changed files with 87 additions and 4 deletions
43
src/steps/one.ts
Normal file
43
src/steps/one.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { useTypes } from 'over.ts/src/index';
|
||||
|
||||
const types = {
|
||||
number: (x: unknown): x is number => typeof x === 'number',
|
||||
bigint: (x: unknown): x is bigint => typeof x === 'bigint'
|
||||
}
|
||||
|
||||
const overload = useTypes(types)
|
||||
|
||||
const negate = overload({
|
||||
'number -> number': (a: number): number => {
|
||||
console.log('Negating number', a)
|
||||
return -a
|
||||
},
|
||||
'bigint -> bigint': (a: bigint): bigint => {
|
||||
console.log('Negating bigint', a)
|
||||
return -a
|
||||
}
|
||||
})
|
||||
|
||||
console.log('Negation of 5 is', negate(5))
|
||||
console.log('Negation of 5n is', negate(5n))
|
||||
|
||||
const add = overload({
|
||||
'number, number -> number': (a: number, b: number): number => {
|
||||
console.log('Adding numbers', a, b)
|
||||
return a+b
|
||||
},
|
||||
'bigint, bigint -> bigint': (a: bigint, b: bigint): bigint => {
|
||||
console.log('Adding bigints', a, b)
|
||||
return a+b
|
||||
}
|
||||
})
|
||||
|
||||
console.log('Sum of 5 and 7 is', add(5,7))
|
||||
console.log('Sum of 5n and 7n is', add(5n, 7n))
|
||||
|
||||
try {
|
||||
//@ts-expect-error
|
||||
console.log('Mixed sum is', add(5n, 7))
|
||||
} catch {
|
||||
console.log('Mixed sum errored as expected.')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue