Created
October 25, 2018 15:38
-
-
Save meatherly/e66015e4e46b3f35883d75c57a6b130d to your computer and use it in GitHub Desktop.
Typescript generics
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
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