[@types/react-redux] add types for hook factories (#39216)

* [@types/react-redux] add types for hook factories

* [@types/react-redux] add test of createSelectorHook+TypedUseSelectorHook
This commit is contained in:
Dylan Vann
2019-10-18 15:34:59 -07:00
committed by Andrew Branch
parent 1d892c020d
commit 674d84d48a
2 changed files with 43 additions and 0 deletions
+25
View File
@@ -13,6 +13,7 @@
// Boris Sergeyev <https://github.com/surgeboris>
// Søren Bruus Frank <https://github.com/soerenbf>
// Jonathan Ziller <https://github.com/mrwolfz>
// Dylan Vann <https://github.com/dylanvann>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@@ -578,4 +579,28 @@ export interface TypedUseSelectorHook<TState> {
*/
export function useStore<S = any, A extends Action = AnyAction>(): Store<S, A>;
/**
* Hook factory, which creates a `useSelector` hook bound to a given context.
*
* @param Context passed to your `<Provider>`.
* @returns A `useSelector` hook bound to the specified context.
*/
export function createSelectorHook(context?: Context<ReactReduxContextValue>): typeof useSelector;
/**
* Hook factory, which creates a `useStore` hook bound to a given context.
*
* @param Context passed to your `<Provider>`.
* @returns A `useStore` hook bound to the specified context.
*/
export function createStoreHook(context?: Context<ReactReduxContextValue>): typeof useStore;
/**
* Hook factory, which creates a `useDispatch` hook bound to a given context.
*
* @param Context passed to your `<Provider>`.
* @returns A `useDispatch` hook bound to the specified context.
*/
export function createDispatchHook(context?: Context<ReactReduxContextValue>): typeof useDispatch;
// tslint:enable:no-unnecessary-generics
+18
View File
@@ -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 = Dispatch<any>>(): TDispatch; <A extends Action<any> = AnyAction>(): Dispatch<A>; }
createDispatchHook();
// $ExpectType <TState, TSelected>(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<RootState> = createSelectorHook();
// $ExpectType <S = any, A extends Action<any> = AnyAction>() => Store<S, A>
createStoreHook();
}
function testConnectedProps() {
interface OwnProps {
own: string;