Skip to content

Instantly share code, notes, and snippets.

@hex13
Created September 20, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hex13/d4c25e3ca92652ceea5579ccbabac49d to your computer and use it in GitHub Desktop.
Save hex13/d4c25e3ca92652ceea5579ccbabac49d to your computer and use it in GitHub Desktop.
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