Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Created March 21, 2019 09:44
Show Gist options
  • Save michaelbromley/572a194d0b9127f39111a2ebf40f5330 to your computer and use it in GitHub Desktop.
Save michaelbromley/572a194d0b9127f39111a2ebf40f5330 to your computer and use it in GitHub Desktop.
Partial application in TypeScript - Pick
/**
* 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