-
-
Save hex13/d4c25e3ca92652ceea5579ccbabac49d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = (cond) => { if (!cond) throw new Error('assertion error') }; | |
type Sum = (a: any, b: any) => number; | |
const addWithNumbers: Sum = (a, b) => a + b; | |
const numbers = { | |
one: 1, | |
two: 2, | |
three: 3 | |
}; | |
const addWithStrings: Sum = (a, b) => numbers[a] + numbers[b]; | |
console.log(addWithNumbers(10, 20)); | |
console.log(addWithStrings('one', 'three')); | |
function perform(add: Sum) { | |
const result = add(120, 3); | |
console.log(result); | |
assert(result === 123); | |
} | |
perform(addWithNumbers); | |
console.log("with strings"); | |
perform(addWithStrings); | |
//-------------------------------------------------- | |
assert(addWithStrings('one', 'three') === 4); | |
assert(addWithNumbers(10, 20) === 30); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment