Skip to content

Commit 5a582cf

Browse files
committed
patch/improvement: added basic TS support for optional selectors
1 parent ff62b3e commit 5a582cf

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

index.d.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference types="redux" />
22
/// <reference types="react" />
33
import { Action, Unsubscribe, AnyAction, Store } from "redux";
4-
import { Component } from "react"
4+
import { Component } from "react";
55

66
/****************************
77
util
@@ -11,7 +11,13 @@ type RestParams<TFunction> = TFunction extends (arg: any, ...args: infer A) => v
1111
/****************************
1212
hook
1313
****************************/
14-
type ReactReduxHook<TState> = () => TState;
14+
/**
15+
* use(), a React hook for the Redux Model's state
16+
* @param selector optional function to select-from or transform the Redux Model's state. See https://react-redux.js.org/api/hooks#useselector
17+
*/
18+
type ReactReduxHookWithOptionalSelector<TState> =
19+
| (<TFunction extends (TState) => any>(f: TFunction) => ReturnType<TFunction>)
20+
| (() => TState);
1521

1622
/****************************
1723
reducers
@@ -45,9 +51,7 @@ interface PayloadAction<TPayload> extends Action<string> {
4551

4652
type SetterDispatcher<TState> = (state: TState) => PayloadAction<TState>;
4753

48-
type Dispatcher<TReducer> = (
49-
...args: RestParams<TReducer>
50-
) => PayloadAction<RestParams<TReducer>[0]>;
54+
type Dispatcher<TReducer> = (...args: RestParams<TReducer>) => PayloadAction<RestParams<TReducer>[0]>;
5155

5256
type Dispatchers<TReducers> = {
5357
[K in keyof TReducers]: Dispatcher<TReducers[K]>;
@@ -71,13 +75,13 @@ type Dispatchers<TReducers> = {
7175
export function createReduxModule<TState>(
7276
reduxStorePropertyName: string,
7377
initialState: TState
74-
): [ReactReduxHook<TState>, SetterDispatcher<TState>, VirtualStore<TState>];
78+
): [ReactReduxHookWithOptionalSelector<TState>, SetterDispatcher<TState>, VirtualStore<TState>];
7579
export function createReduxModule<TState, TReducers extends Reducers<TState>>(
7680
reduxStorePropertyName: string,
7781
initialState: TState,
7882
reducers: TReducers
7983
): [
80-
ReactReduxHook<TState>,
84+
ReactReduxHookWithOptionalSelector<TState>,
8185
Readonly<Dispatchers<TReducers>>,
8286
VirtualStoreWithReducers<TState, TReducers>
8387
];
@@ -98,13 +102,13 @@ export function createReduxModule<TState, TReducers extends Reducers<TState>>(
98102
export function useRedux<TState>(
99103
reduxStorePropertyName: string,
100104
initialState: TState
101-
): [ReactReduxHook<TState>, SetterDispatcher<TState>, VirtualStore<TState>];
105+
): [ReactReduxHookWithOptionalSelector<TState>, SetterDispatcher<TState>, VirtualStore<TState>];
102106
export function useRedux<TState, TReducers extends Reducers<TState>>(
103107
reduxStorePropertyName: string,
104108
initialState: TState,
105109
reducers: TReducers
106110
): [
107-
ReactReduxHook<TState>,
111+
ReactReduxHookWithOptionalSelector<TState>,
108112
Readonly<Dispatchers<TReducers>>,
109113
VirtualStoreWithReducers<TState, TReducers>
110114
];
@@ -113,15 +117,15 @@ export function useRedux<TState, TReducers extends Reducers<TState>>(
113117
other hooks-for-redux functions
114118
****************************/
115119
export interface ReduxStoreWithInjectReducers extends Store {
116-
injectReducer(key:string, reducer:Reducer<object>) : void
120+
injectReducer(key: string, reducer: Reducer<object>): void;
117121
}
118122

119123
/**
120124
* Auto-vivifies a store if setStore has not been called. Otherwise, it returns the store passed to setStore.
121125
*
122126
* @returns current or newly crated store
123127
*/
124-
export function getStore() : ReduxStoreWithInjectReducers
128+
export function getStore(): ReduxStoreWithInjectReducers;
125129

126130
/**
127131
* Call setStore to provide your own store for hooks-for-redux to use. You'll need to use this if you want to use middleware.
@@ -130,7 +134,7 @@ export function getStore() : ReduxStoreWithInjectReducers
130134
*
131135
* @returns store
132136
*/
133-
export function setStore(store : ReduxStoreWithInjectReducers) : ReduxStoreWithInjectReducers
137+
export function setStore(store: ReduxStoreWithInjectReducers): ReduxStoreWithInjectReducers;
134138

135139
/**
136140
* Creates a Redux store that holds the state tree.
@@ -156,7 +160,11 @@ export function setStore(store : ReduxStoreWithInjectReducers) : ReduxStoreWithI
156160
* @returns A Redux store that lets you read the state, dispatch actions and
157161
* subscribe to changes.
158162
*/
159-
export function createStore<TState>(reducers: Reducers<object>, initialState?: TState, enhancer?: any) : ReduxStoreWithInjectReducers
163+
export function createStore<TState>(
164+
reducers: Reducers<object>,
165+
initialState?: TState,
166+
enhancer?: any
167+
): ReduxStoreWithInjectReducers;
160168

161169
export interface ProviderProps<A extends Action = AnyAction> {
162170
/**
@@ -174,7 +182,7 @@ export interface ProviderProps<A extends Action = AnyAction> {
174182
/**
175183
* Makes the Redux store available to the connect() calls in the component hierarchy below.
176184
*/
177-
export class Provider<A extends Action = AnyAction> extends Component<ProviderProps<A>> { }
185+
export class Provider<A extends Action = AnyAction> extends Component<ProviderProps<A>> {}
178186

179187
/****************************
180188
type testing

0 commit comments

Comments
 (0)