Created
March 21, 2019 09:44
-
-
Save michaelbromley/572a194d0b9127f39111a2ebf40f5330 to your computer and use it in GitHub Desktop.
Partial application in TypeScript - Pick
This file contains 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
/** | |
* Typing partial application in TypeScript! | |
* | |
* Here's a "pick" function which takes some property names and returns a function which will pick those | |
* from an object passed to it. | |
*/ | |
export function pick<T extends string>(props: T[]): <U>(input: U) => { [K in T]: K extends keyof U ? U[K]: never } { | |
return {} as any; // implementation not important for now | |
} | |
interface Person { | |
name: string; | |
age: number; | |
friends: Person[] | |
} | |
interface Dog { | |
name: string; | |
breed: string; | |
friends: Dog[]; | |
} | |
interface Cat { | |
name: string; | |
} | |
let joe: Person = {} as any; | |
let fido: Dog = {} as any; | |
let felix: Cat = {} as any; | |
const pickFriends = pick(['friends']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment