1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import {
|
23 | Component,
|
24 | ComponentType,
|
25 | StatelessComponent,
|
26 | Context,
|
27 | NamedExoticComponent
|
28 | } from 'react';
|
29 |
|
30 | import {
|
31 | Action,
|
32 | ActionCreator,
|
33 | AnyAction,
|
34 | Dispatch,
|
35 | Store
|
36 | } from 'redux';
|
37 |
|
38 | import hoistNonReactStatics = require('hoist-non-react-statics');
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | export interface DefaultRootState {}
|
48 |
|
49 | export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
|
50 | export type RootStateOrAny = AnyIfEmpty<DefaultRootState>;
|
51 |
|
52 |
|
53 | export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
54 |
|
55 | export interface DispatchProp<A extends Action = AnyAction> {
|
56 | dispatch: Dispatch<A>;
|
57 | }
|
58 |
|
59 | export type AdvancedComponentDecorator<TProps, TOwnProps> =
|
60 | (component: ComponentType<TProps>) => NamedExoticComponent<TOwnProps>;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export type Matching<InjectedProps, DecorationTargetProps> = {
|
75 | [P in keyof DecorationTargetProps]: P extends keyof InjectedProps
|
76 | ? InjectedProps[P] extends DecorationTargetProps[P]
|
77 | ? DecorationTargetProps[P]
|
78 | : InjectedProps[P]
|
79 | : DecorationTargetProps[P];
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export type Shared<
|
93 | InjectedProps,
|
94 | DecorationTargetProps
|
95 | > = {
|
96 | [P in Extract<keyof InjectedProps, keyof DecorationTargetProps>]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never;
|
97 | };
|
98 |
|
99 |
|
100 | export type GetProps<C> = C extends ComponentType<infer P> ? P : never;
|
101 |
|
102 |
|
103 |
|
104 | export type ConnectedComponent<
|
105 | C extends ComponentType<any>,
|
106 | P
|
107 | > = NamedExoticComponent<JSX.LibraryManagedAttributes<C, P>> & hoistNonReactStatics.NonReactStatics<C> & {
|
108 | WrappedComponent: C;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> =
|
115 | <C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
|
116 | component: C
|
117 | ) => ConnectedComponent<C, Omit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> & TNeedsProps>;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export type InferableComponentEnhancer<TInjectedProps> =
|
123 | InferableComponentEnhancerWithProps<TInjectedProps, {}>;
|
124 |
|
125 | export type InferThunkActionCreatorType<TActionCreator extends (...args: any[]) => any> =
|
126 | TActionCreator extends (...args: infer TParams) => (...args: any[]) => infer TReturn
|
127 | ? (...args: TParams) => TReturn
|
128 | : TActionCreator;
|
129 |
|
130 | export type HandleThunkActionCreator<TActionCreator> =
|
131 | TActionCreator extends (...args: any[]) => any
|
132 | ? InferThunkActionCreatorType<TActionCreator>
|
133 | : TActionCreator;
|
134 |
|
135 |
|
136 |
|
137 | export type ResolveThunks<TDispatchProps> =
|
138 | TDispatchProps extends { [key: string]: any }
|
139 | ? {
|
140 | [C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>
|
141 | }
|
142 | : TDispatchProps;
|
143 |
|
144 |
|
145 |
|
146 | export type ResolveArrayThunks<TDispatchProps extends ReadonlyArray<any>> =
|
147 | TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, infer A8, infer A9]
|
148 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
149 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>, HandleThunkActionCreator<A8>, HandleThunkActionCreator<A9>]
|
150 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, infer A8]
|
151 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
152 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>, HandleThunkActionCreator<A8>]
|
153 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7]
|
154 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>,
|
155 | HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>, HandleThunkActionCreator<A7>]
|
156 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6]
|
157 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>, HandleThunkActionCreator<A5>, HandleThunkActionCreator<A6>]
|
158 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5]
|
159 | ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>, HandleThunkActionCreator<A5>]
|
160 | : TDispatchProps extends [infer A1, infer A2, infer A3, infer A4] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>, HandleThunkActionCreator<A4>]
|
161 | : TDispatchProps extends [infer A1, infer A2, infer A3] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>, HandleThunkActionCreator<A3>]
|
162 | : TDispatchProps extends [infer A1, infer A2] ? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>]
|
163 | : TDispatchProps extends [infer A1] ? [HandleThunkActionCreator<A1>]
|
164 | : TDispatchProps extends Array<infer A> ? Array<HandleThunkActionCreator<A>>
|
165 | : TDispatchProps extends ReadonlyArray<infer A> ? ReadonlyArray<HandleThunkActionCreator<A>>
|
166 | : never
|
167 | ;
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | export interface Connect {
|
189 |
|
190 | (): InferableComponentEnhancer<DispatchProp>;
|
191 |
|
192 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = DefaultRootState>(
|
193 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>
|
194 | ): InferableComponentEnhancerWithProps<TStateProps & DispatchProp, TOwnProps>;
|
195 |
|
196 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
|
197 | mapStateToProps: null | undefined,
|
198 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
|
199 | ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
|
200 |
|
201 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
|
202 | mapStateToProps: null | undefined,
|
203 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
204 | ): InferableComponentEnhancerWithProps<
|
205 | ResolveThunks<TDispatchProps>,
|
206 | TOwnProps
|
207 | >;
|
208 |
|
209 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultRootState>(
|
210 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
211 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>
|
212 | ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
|
213 |
|
214 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultRootState>(
|
215 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
216 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
217 | ): InferableComponentEnhancerWithProps<
|
218 | TStateProps & ResolveThunks<TDispatchProps>,
|
219 | TOwnProps
|
220 | >;
|
221 |
|
222 | <no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
|
223 | mapStateToProps: null | undefined,
|
224 | mapDispatchToProps: null | undefined,
|
225 | mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
|
226 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
227 |
|
228 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}, State = DefaultRootState>(
|
229 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
230 | mapDispatchToProps: null | undefined,
|
231 | mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
|
232 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
233 |
|
234 | <no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
|
235 | mapStateToProps: null | undefined,
|
236 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
237 | mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
|
238 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
239 |
|
240 | <TStateProps = {}, no_dispatch = {}, TOwnProps = {}, State = DefaultRootState>(
|
241 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
242 | mapDispatchToProps: null | undefined,
|
243 | mergeProps: null | undefined,
|
244 | options: Options<State, TStateProps, TOwnProps>
|
245 | ): InferableComponentEnhancerWithProps<DispatchProp & TStateProps, TOwnProps>;
|
246 |
|
247 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
|
248 | mapStateToProps: null | undefined,
|
249 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
|
250 | mergeProps: null | undefined,
|
251 | options: Options<{}, TStateProps, TOwnProps>
|
252 | ): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
|
253 |
|
254 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
|
255 | mapStateToProps: null | undefined,
|
256 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
257 | mergeProps: null | undefined,
|
258 | options: Options<{}, TStateProps, TOwnProps>
|
259 | ): InferableComponentEnhancerWithProps<
|
260 | ResolveThunks<TDispatchProps>,
|
261 | TOwnProps
|
262 | >;
|
263 |
|
264 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultRootState>(
|
265 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
266 | mapDispatchToProps: MapDispatchToPropsNonObject<TDispatchProps, TOwnProps>,
|
267 | mergeProps: null | undefined,
|
268 | options: Options<State, TStateProps, TOwnProps>
|
269 | ): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
|
270 |
|
271 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = DefaultRootState>(
|
272 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
273 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
274 | mergeProps: null | undefined,
|
275 | options: Options<State, TStateProps, TOwnProps>
|
276 | ): InferableComponentEnhancerWithProps<
|
277 | TStateProps & ResolveThunks<TDispatchProps>,
|
278 | TOwnProps
|
279 | >;
|
280 |
|
281 | <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}, State = DefaultRootState>(
|
282 | mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
283 | mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
284 | mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
|
285 | options?: Options<State, TStateProps, TOwnProps, TMergedProps>
|
286 | ): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
|
287 |
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | export type ConnectedProps<TConnector> =
|
294 | TConnector extends InferableComponentEnhancerWithProps<infer TInjectedProps, any>
|
295 | ? TInjectedProps
|
296 | : never;
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | export const connect: Connect;
|
302 |
|
303 | export type MapStateToProps<TStateProps, TOwnProps, State = DefaultRootState> =
|
304 | (state: State, ownProps: TOwnProps) => TStateProps;
|
305 |
|
306 | export type MapStateToPropsFactory<TStateProps, TOwnProps, State = DefaultRootState> =
|
307 | (initialState: State, ownProps: TOwnProps) => MapStateToProps<TStateProps, TOwnProps, State>;
|
308 |
|
309 | export type MapStateToPropsParam<TStateProps, TOwnProps, State = DefaultRootState> =
|
310 | MapStateToPropsFactory<TStateProps, TOwnProps, State> | MapStateToProps<TStateProps, TOwnProps, State> | null | undefined;
|
311 |
|
312 | export type MapDispatchToPropsFunction<TDispatchProps, TOwnProps> =
|
313 | (dispatch: Dispatch<Action>, ownProps: TOwnProps) => TDispatchProps;
|
314 |
|
315 | export type MapDispatchToProps<TDispatchProps, TOwnProps> =
|
316 | MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | TDispatchProps;
|
317 |
|
318 | export type MapDispatchToPropsFactory<TDispatchProps, TOwnProps> =
|
319 | (dispatch: Dispatch<Action>, ownProps: TOwnProps) => MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
|
320 |
|
321 | export type MapDispatchToPropsParam<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToProps<TDispatchProps, TOwnProps>;
|
322 |
|
323 | export type MapDispatchToPropsNonObject<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToPropsFunction<TDispatchProps, TOwnProps>;
|
324 |
|
325 | export type MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps> =
|
326 | (stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps) => TMergedProps;
|
327 |
|
328 | export interface Options<State = DefaultRootState, TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | pure?: boolean;
|
337 |
|
338 | |
339 |
|
340 |
|
341 |
|
342 | areStatesEqual?: (nextState: State, prevState: State) => boolean;
|
343 |
|
344 | |
345 |
|
346 |
|
347 |
|
348 | areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean;
|
349 |
|
350 | |
351 |
|
352 |
|
353 |
|
354 | areStatePropsEqual?: (nextStateProps: TStateProps, prevStateProps: TStateProps) => boolean;
|
355 |
|
356 | |
357 |
|
358 |
|
359 |
|
360 | areMergedPropsEqual?: (nextMergedProps: TMergedProps, prevMergedProps: TMergedProps) => boolean;
|
361 |
|
362 | |
363 |
|
364 |
|
365 |
|
366 |
|
367 | forwardRef?: boolean;
|
368 | }
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
|
381 |
|
382 | selectorFactory: SelectorFactory<S, TProps, TOwnProps, TFactoryOptions>,
|
383 | connectOptions?: ConnectOptions & TFactoryOptions
|
384 | ): AdvancedComponentDecorator<TProps, TOwnProps>;
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | export type SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> =
|
395 | (dispatch: Dispatch<Action>, factoryOptions: TFactoryOptions) => Selector<S, TProps, TOwnProps>;
|
396 |
|
397 | export type Selector<S, TProps, TOwnProps = null> = TOwnProps extends null | undefined
|
398 | ? (state: S) => TProps
|
399 | : (state: S, ownProps: TOwnProps) => TProps;
|
400 |
|
401 | export interface ConnectOptions {
|
402 | |
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | getDisplayName?: (componentName: string) => string;
|
410 | |
411 |
|
412 |
|
413 |
|
414 |
|
415 | methodName?: string;
|
416 | |
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | renderCountProp?: string;
|
424 | |
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 | shouldHandleStateChanges?: boolean;
|
431 | |
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | storeKey?: string;
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 | withRef?: boolean;
|
444 | |
445 |
|
446 |
|
447 |
|
448 |
|
449 | context?: Context<ReactReduxContextValue>;
|
450 | }
|
451 |
|
452 | export interface ReactReduxContextValue<SS = any, A extends Action = AnyAction> {
|
453 | store: Store<SS, A>;
|
454 | storeState: SS;
|
455 | }
|
456 |
|
457 | export interface ProviderProps<A extends Action = AnyAction> {
|
458 | |
459 |
|
460 |
|
461 | store: Store<any, A>;
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 | context?: Context<ReactReduxContextValue>;
|
468 | }
|
469 |
|
470 |
|
471 |
|
472 |
|
473 | export class Provider<A extends Action = AnyAction> extends Component<ProviderProps<A>> { }
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | export const ReactReduxContext: Context<ReactReduxContextValue>;
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | export function batch(cb: () => void): void;
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 | export function shallowEqual<T>(left: T, right: any): boolean;
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 | export function useDispatch<TDispatch = Dispatch<any>>(): TDispatch;
|
525 | export function useDispatch<A extends Action = AnyAction>(): Dispatch<A>;
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 | export function useSelector<TState = DefaultRootState, TSelected = unknown>(
|
556 | selector: (state: TState) => TSelected,
|
557 | equalityFn?: (left: TSelected, right: TSelected) => boolean
|
558 | ): TSelected;
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 | export interface TypedUseSelectorHook<TState> {
|
574 | <TSelected>(
|
575 | selector: (state: TState) => TSelected,
|
576 | equalityFn?: (left: TSelected, right: TSelected) => boolean
|
577 | ): TSelected;
|
578 | }
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | export function useStore<S = RootStateOrAny, A extends Action = AnyAction>(): Store<S, A>;
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 | export function createSelectorHook(context?: Context<ReactReduxContextValue>): typeof useSelector;
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 | export function createStoreHook(context?: Context<ReactReduxContextValue>): typeof useStore;
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | export function createDispatchHook(context?: Context<ReactReduxContextValue>): typeof useDispatch;
|
620 |
|
621 |
|