Last active
November 20, 2019 19:28
-
-
Save kdshop/201192b10a47477512d452edf55ea66c to your computer and use it in GitHub Desktop.
generic typesafe state projector - proxy usecase
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
// Instead of selector helpers like `export const getHasError = (state: AppUIState) => state.hasError;` | |
// use this. The typing is compatible with ngrx and gives you type hinting also (just dont forget to provide generic!) | |
const fromState = <T = {[key: string]: any}>() => { | |
return (new Proxy({}, { | |
get: (target: {}, p: string | number | symbol) => (state) => state[p] | |
})) as {[key in keyof T]: (state: T) => T[key]}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment