Skip to content

Instantly share code, notes, and snippets.

@gtkatakura
Created May 6, 2018 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtkatakura/ae072a327a885257ac964c229cdf1a0d to your computer and use it in GitHub Desktop.
Save gtkatakura/ae072a327a885257ac964c229cdf1a0d to your computer and use it in GitHub Desktop.
TypeScript + Curry
type ThemeColor = string;
type Theme = { id: number, color: ThemeColor }
type State = { themes?: [Theme] }
interface CurriedFunction1<T1, R> {
(t1: T1): R;
}
interface CurriedFunction2<T1, T2, R> {
(t1: T1): CurriedFunction1<T2, R>;
(t1: T1, t2: T2): R;
}
type Curry<T> =
T extends () => any ? never[] :
T extends (t1: infer T1) => infer R ? CurriedFunction1<T1, R> :
T extends (t1: infer T1, t2: infer T2) => infer R ? CurriedFunction2<T1, T2, R> : any
const selectors = {
getTheme(state: State, color: ThemeColor): Theme {
return { id: 1, color: 'red' };
}
}
type Selectors = {
[P in keyof typeof selectors]: Curry<typeof selectors[P]>;
}
const example = (selectors: Selectors) => {
const theme = selectors.getTheme({})("red");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment