Skip to content

Instantly share code, notes, and snippets.

@meatherly
Created October 25, 2018 15:38
Show Gist options
  • Save meatherly/e66015e4e46b3f35883d75c57a6b130d to your computer and use it in GitHub Desktop.
Save meatherly/e66015e4e46b3f35883d75c57a6b130d to your computer and use it in GitHub Desktop.
Typescript generics
function map<A, B>(arr: A[], fn: (el: A) => B): B[] {
return arr.map(fn);
}
// manual annotation
map<number, void>(['string'], el => el * 2)
// inferred annotation
map([1,2,4], (num) => num * 2)
function mapNumberArray(arr: number[], fn: (el: number) => number[]) {
return arr.map(fn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment