Skip to content

Instantly share code, notes, and snippets.

@kdshop
Last active November 20, 2019 19:28
Show Gist options
  • Save kdshop/201192b10a47477512d452edf55ea66c to your computer and use it in GitHub Desktop.
Save kdshop/201192b10a47477512d452edf55ea66c to your computer and use it in GitHub Desktop.
generic typesafe state projector - proxy usecase
// 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