UNPKG

4.66 kBTypeScriptView Raw
1import { ClassAttributes, ComponentClass, ComponentType, FunctionComponent } from 'react';
2import { Action, AnyAction, Dispatch } from 'redux';
3import type { NonReactStatics } from 'hoist-non-react-statics';
4import type { ConnectProps } from './components/connect';
5export declare type FixTypeLater = any;
6export declare type EqualityFn<T> = (a: T, b: T) => boolean;
7export declare type ExtendedEqualityFn<T, P> = (a: T, b: T, c: P, d: P) => boolean;
8export declare type AnyIfEmpty<T extends object> = keyof T extends never ? any : T;
9export declare type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
10export interface DispatchProp<A extends Action = AnyAction> {
11 dispatch: Dispatch<A>;
12}
13/**
14 * A property P will be present if:
15 * - it is present in DecorationTargetProps
16 *
17 * Its value will be dependent on the following conditions
18 * - if property P is present in InjectedProps and its definition extends the definition
19 * in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
20 * - if property P is not present in InjectedProps then its definition will be that of
21 * DecorationTargetProps[P]
22 * - if property P is present in InjectedProps but does not extend the
23 * DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
24 */
25export declare type Matching<InjectedProps, DecorationTargetProps> = {
26 [P in keyof DecorationTargetProps]: P extends keyof InjectedProps ? InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : InjectedProps[P] : DecorationTargetProps[P];
27};
28/**
29 * a property P will be present if :
30 * - it is present in both DecorationTargetProps and InjectedProps
31 * - InjectedProps[P] can satisfy DecorationTargetProps[P]
32 * ie: decorated component can accept more types than decorator is injecting
33 *
34 * For decoration, inject props or ownProps are all optionally
35 * required by the decorated (right hand side) component.
36 * But any property required by the decorated component must be satisfied by the injected property.
37 */
38export declare type Shared<InjectedProps, DecorationTargetProps> = {
39 [P in Extract<keyof InjectedProps, keyof DecorationTargetProps>]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never;
40};
41export declare type GetProps<C> = C extends ComponentType<infer P> ? C extends ComponentClass<P> ? ClassAttributes<InstanceType<C>> & P : P : never;
42export declare type GetLibraryManagedProps<C> = JSX.LibraryManagedAttributes<C, GetProps<C>>;
43export declare type ConnectedComponent<C extends ComponentType<any>, P> = FunctionComponent<P> & NonReactStatics<C> & {
44 WrappedComponent: C;
45};
46export declare type ConnectPropsMaybeWithoutContext<TActualOwnProps> = TActualOwnProps extends {
47 context: any;
48} ? Omit<ConnectProps, 'context'> : ConnectProps;
49declare type Identity<T> = T;
50export declare type Mapped<T> = Identity<{
51 [k in keyof T]: T[k];
52}>;
53export declare type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> = <C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(component: C) => ConnectedComponent<C, Mapped<DistributiveOmit<GetLibraryManagedProps<C>, keyof Shared<TInjectedProps, GetLibraryManagedProps<C>>> & TNeedsProps & ConnectPropsMaybeWithoutContext<TNeedsProps & GetProps<C>>>>;
54export declare type InferableComponentEnhancer<TInjectedProps> = InferableComponentEnhancerWithProps<TInjectedProps, {}>;
55export declare type InferThunkActionCreatorType<TActionCreator extends (...args: any[]) => any> = TActionCreator extends (...args: infer TParams) => (...args: any[]) => infer TReturn ? (...args: TParams) => TReturn : TActionCreator;
56export declare type HandleThunkActionCreator<TActionCreator> = TActionCreator extends (...args: any[]) => any ? InferThunkActionCreatorType<TActionCreator> : TActionCreator;
57export declare type ResolveThunks<TDispatchProps> = TDispatchProps extends {
58 [key: string]: any;
59} ? {
60 [C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>;
61} : TDispatchProps;
62/**
63 * This interface allows you to easily create a hook that is properly typed for your
64 * store's root state.
65 *
66 * @example
67 *
68 * interface RootState {
69 * property: string;
70 * }
71 *
72 * const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector;
73 */
74export interface TypedUseSelectorHook<TState> {
75 <TSelected>(selector: (state: TState) => TSelected, equalityFn?: EqualityFn<NoInfer<TSelected>>): TSelected;
76}
77export declare type NoInfer<T> = [T][T extends any ? 0 : never];
78export {};
79
\No newline at end of file