diff --git a/types/react-redux/index.d.ts b/types/react-redux/index.d.ts index 997f045a73..8726d895da 100644 --- a/types/react-redux/index.d.ts +++ b/types/react-redux/index.d.ts @@ -13,6 +13,7 @@ // Boris Sergeyev // Søren Bruus Frank // Jonathan Ziller +// Dylan Vann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 @@ -578,4 +579,28 @@ export interface TypedUseSelectorHook { */ export function useStore(): Store; +/** + * Hook factory, which creates a `useSelector` hook bound to a given context. + * + * @param Context passed to your ``. + * @returns A `useSelector` hook bound to the specified context. + */ +export function createSelectorHook(context?: Context): typeof useSelector; + +/** + * Hook factory, which creates a `useStore` hook bound to a given context. + * + * @param Context passed to your ``. + * @returns A `useStore` hook bound to the specified context. + */ +export function createStoreHook(context?: Context): typeof useStore; + +/** + * Hook factory, which creates a `useDispatch` hook bound to a given context. + * + * @param Context passed to your ``. + * @returns A `useDispatch` hook bound to the specified context. + */ +export function createDispatchHook(context?: Context): typeof useDispatch; + // tslint:enable:no-unnecessary-generics diff --git a/types/react-redux/react-redux-tests.tsx b/types/react-redux/react-redux-tests.tsx index 285d46e53c..5399b3657d 100644 --- a/types/react-redux/react-redux-tests.tsx +++ b/types/react-redux/react-redux-tests.tsx @@ -26,6 +26,9 @@ import { useDispatch, useSelector, useStore, + createDispatchHook, + createSelectorHook, + createStoreHook, TypedUseSelectorHook, } from 'react-redux'; import objectAssign = require('object-assign'); @@ -1393,6 +1396,21 @@ function testUseStore() { typedState.things.stuff; // $ExpectError } +// These should match the types of the hooks. +function testCreateHookFunctions() { + // $ExpectType { >(): TDispatch; = AnyAction>(): Dispatch; } + createDispatchHook(); + // $ExpectType (selector: (state: TState) => TSelected, equalityFn?: ((left: TSelected, right: TSelected) => boolean) | undefined) => TSelected + createSelectorHook(); + interface RootState { + property: string; + } + // Should be able to create a version typed for a specific root state. + const useTypedSelector: TypedUseSelectorHook = createSelectorHook(); + // $ExpectType = AnyAction>() => Store + createStoreHook(); +} + function testConnectedProps() { interface OwnProps { own: string;