Created
May 6, 2018 18:13
-
-
Save gtkatakura/ae072a327a885257ac964c229cdf1a0d to your computer and use it in GitHub Desktop.
TypeScript + Curry
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
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