mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-16 23:10:29 +00:00
[@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:
committed by
Andrew Branch
parent
1d892c020d
commit
674d84d48a
Vendored
+25
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user