Skip to content

Instantly share code, notes, and snippets.

@matthew-gerstman
Created August 23, 2018 14:39
Show Gist options
  • Save matthew-gerstman/0f5d8c39386d508f53cf4d27fd140619 to your computer and use it in GitHub Desktop.
Save matthew-gerstman/0f5d8c39386d508f53cf4d27fd140619 to your computer and use it in GitHub Desktop.
// redux-utils/selectors.ts
import { FullStoreShape, NamespaceKey, StoreShape } from "./types";
export function getStateAtNamespaceKey<T extends NamespaceKey>(
state: StoreShape,
namespace: T
): FullStoreShape[T] {
const namespaceState = state[namespace];
if (!namespaceState) {
throw new Error(
`Attempted to access state for an unregistered namespace at key ${namespace}`
);
}
// We need to explicitly say this is not undefined because TypeScript doesn't
// recognize the thrown error will prevent us from ever getting here.
return namespaceState!;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment