Skip to content

Instantly share code, notes, and snippets.

@kakajika
Created July 3, 2015 18:52
Show Gist options
  • Save kakajika/959d1c8d31d3b065fe51 to your computer and use it in GitHub Desktop.
Save kakajika/959d1c8d31d3b065fe51 to your computer and use it in GitHub Desktop.
Array.prototype.flatMap method in TypeScript.
interface Array<T> {
flatMap<E>(callback: (t: T) => Array<E>): Array<E>
}
Object.defineProperty(Array.prototype, 'flatMap', {
value: function(f: Function) {
return this.reduce((ys: any, x: any) => {
return ys.concat(f.call(this, x))
}, [])
},
enumerable: false,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment