| 1 | import type {
|
|---|
| 2 | ClassAttributes,
|
|---|
| 3 | ComponentClass,
|
|---|
| 4 | ComponentType,
|
|---|
| 5 | FunctionComponent,
|
|---|
| 6 | JSX,
|
|---|
| 7 | } from 'react'
|
|---|
| 8 |
|
|---|
| 9 | import type { Action, UnknownAction, Dispatch } from 'redux'
|
|---|
| 10 |
|
|---|
| 11 | import type { NonReactStatics } from './utils/hoistStatics'
|
|---|
| 12 |
|
|---|
| 13 | import type { ConnectProps } from './components/connect'
|
|---|
| 14 |
|
|---|
| 15 | import type { UseSelectorOptions } from './hooks/useSelector'
|
|---|
| 16 |
|
|---|
| 17 | export type FixTypeLater = any
|
|---|
| 18 |
|
|---|
| 19 | export type EqualityFn<T> = (a: T, b: T) => boolean
|
|---|
| 20 |
|
|---|
| 21 | export type ExtendedEqualityFn<T, P> = (a: T, b: T, c: P, d: P) => boolean
|
|---|
| 22 |
|
|---|
| 23 | export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T
|
|---|
| 24 |
|
|---|
| 25 | export type DistributiveOmit<T, K extends keyof T> = T extends unknown
|
|---|
| 26 | ? Omit<T, K>
|
|---|
| 27 | : never
|
|---|
| 28 |
|
|---|
| 29 | export interface DispatchProp<A extends Action<string> = UnknownAction> {
|
|---|
| 30 | dispatch: Dispatch<A>
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * A property P will be present if:
|
|---|
| 35 | * - it is present in DecorationTargetProps
|
|---|
| 36 | *
|
|---|
| 37 | * Its value will be dependent on the following conditions
|
|---|
| 38 | * - if property P is present in InjectedProps and its definition extends the definition
|
|---|
| 39 | * in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
|
|---|
| 40 | * - if property P is not present in InjectedProps then its definition will be that of
|
|---|
| 41 | * DecorationTargetProps[P]
|
|---|
| 42 | * - if property P is present in InjectedProps but does not extend the
|
|---|
| 43 | * DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
|
|---|
| 44 | */
|
|---|
| 45 | export type Matching<InjectedProps, DecorationTargetProps> = {
|
|---|
| 46 | [P in keyof DecorationTargetProps]: P extends keyof InjectedProps
|
|---|
| 47 | ? InjectedProps[P] extends DecorationTargetProps[P]
|
|---|
| 48 | ? DecorationTargetProps[P]
|
|---|
| 49 | : InjectedProps[P]
|
|---|
| 50 | : DecorationTargetProps[P]
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * a property P will be present if :
|
|---|
| 55 | * - it is present in both DecorationTargetProps and InjectedProps
|
|---|
| 56 | * - InjectedProps[P] can satisfy DecorationTargetProps[P]
|
|---|
| 57 | * ie: decorated component can accept more types than decorator is injecting
|
|---|
| 58 | *
|
|---|
| 59 | * For decoration, inject props or ownProps are all optionally
|
|---|
| 60 | * required by the decorated (right hand side) component.
|
|---|
| 61 | * But any property required by the decorated component must be satisfied by the injected property.
|
|---|
| 62 | */
|
|---|
| 63 | export type Shared<InjectedProps, DecorationTargetProps> = {
|
|---|
| 64 | [P in Extract<
|
|---|
| 65 | keyof InjectedProps,
|
|---|
| 66 | keyof DecorationTargetProps
|
|---|
| 67 | >]?: InjectedProps[P] extends DecorationTargetProps[P]
|
|---|
| 68 | ? DecorationTargetProps[P]
|
|---|
| 69 | : never
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // Infers prop type from component C
|
|---|
| 73 | export type GetProps<C> =
|
|---|
| 74 | C extends ComponentType<infer P>
|
|---|
| 75 | ? C extends ComponentClass<P>
|
|---|
| 76 | ? ClassAttributes<InstanceType<C>> & P
|
|---|
| 77 | : P
|
|---|
| 78 | : never
|
|---|
| 79 |
|
|---|
| 80 | // Applies LibraryManagedAttributes (proper handling of defaultProps
|
|---|
| 81 | // and propTypes).
|
|---|
| 82 | export type GetLibraryManagedProps<C> = JSX.LibraryManagedAttributes<
|
|---|
| 83 | C,
|
|---|
| 84 | GetProps<C>
|
|---|
| 85 | >
|
|---|
| 86 |
|
|---|
| 87 | // Applies LibraryManagedAttributes (proper handling of defaultProps
|
|---|
| 88 | // and propTypes), as well as defines WrappedComponent.
|
|---|
| 89 | export type ConnectedComponent<
|
|---|
| 90 | C extends ComponentType<any>,
|
|---|
| 91 | P,
|
|---|
| 92 | > = FunctionComponent<P> &
|
|---|
| 93 | NonReactStatics<C> & {
|
|---|
| 94 | WrappedComponent: C
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | export type ConnectPropsMaybeWithoutContext<TActualOwnProps> =
|
|---|
| 98 | TActualOwnProps extends { context: any }
|
|---|
| 99 | ? Omit<ConnectProps, 'context'>
|
|---|
| 100 | : ConnectProps
|
|---|
| 101 |
|
|---|
| 102 | type Identity<T> = T
|
|---|
| 103 | export type Mapped<T> = Identity<{ [k in keyof T]: T[k] }>
|
|---|
| 104 |
|
|---|
| 105 | // Injects props and removes them from the prop requirements.
|
|---|
| 106 | // Will not pass through the injected props if they are passed in during
|
|---|
| 107 | // render. Also adds new prop requirements from TNeedsProps.
|
|---|
| 108 | // Uses distributive omit to preserve discriminated unions part of original prop type.
|
|---|
| 109 | // Note> Most of the time TNeedsProps is empty, because the overloads in `Connect`
|
|---|
| 110 | // just pass in `{}`. The real props we need come from the component.
|
|---|
| 111 | export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> = <
|
|---|
| 112 | C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>,
|
|---|
| 113 | >(
|
|---|
| 114 | component: C,
|
|---|
| 115 | ) => ConnectedComponent<
|
|---|
| 116 | C,
|
|---|
| 117 | Mapped<
|
|---|
| 118 | DistributiveOmit<
|
|---|
| 119 | GetLibraryManagedProps<C>,
|
|---|
| 120 | keyof Shared<TInjectedProps, GetLibraryManagedProps<C>>
|
|---|
| 121 | > &
|
|---|
| 122 | TNeedsProps &
|
|---|
| 123 | ConnectPropsMaybeWithoutContext<TNeedsProps & GetProps<C>>
|
|---|
| 124 | >
|
|---|
| 125 | >
|
|---|
| 126 |
|
|---|
| 127 | // Injects props and removes them from the prop requirements.
|
|---|
| 128 | // Will not pass through the injected props if they are passed in during
|
|---|
| 129 | // render.
|
|---|
| 130 | export type InferableComponentEnhancer<TInjectedProps> =
|
|---|
| 131 | InferableComponentEnhancerWithProps<TInjectedProps, {}>
|
|---|
| 132 |
|
|---|
| 133 | export type InferThunkActionCreatorType<
|
|---|
| 134 | TActionCreator extends (...args: any[]) => any,
|
|---|
| 135 | > = TActionCreator extends (
|
|---|
| 136 | ...args: infer TParams
|
|---|
| 137 | ) => (...args: any[]) => infer TReturn
|
|---|
| 138 | ? (...args: TParams) => TReturn
|
|---|
| 139 | : TActionCreator
|
|---|
| 140 |
|
|---|
| 141 | export type HandleThunkActionCreator<TActionCreator> = TActionCreator extends (
|
|---|
| 142 | ...args: any[]
|
|---|
| 143 | ) => any
|
|---|
| 144 | ? InferThunkActionCreatorType<TActionCreator>
|
|---|
| 145 | : TActionCreator
|
|---|
| 146 |
|
|---|
| 147 | // redux-thunk middleware returns thunk's return value from dispatch call
|
|---|
| 148 | // https://github.com/reduxjs/redux-thunk#composition
|
|---|
| 149 | export type ResolveThunks<TDispatchProps> = TDispatchProps extends {
|
|---|
| 150 | [key: string]: any
|
|---|
| 151 | }
|
|---|
| 152 | ? {
|
|---|
| 153 | [C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>
|
|---|
| 154 | }
|
|---|
| 155 | : TDispatchProps
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * This interface allows you to easily create a hook that is properly typed for your
|
|---|
| 159 | * store's root state.
|
|---|
| 160 | *
|
|---|
| 161 | * @example
|
|---|
| 162 | *
|
|---|
| 163 | * interface RootState {
|
|---|
| 164 | * property: string;
|
|---|
| 165 | * }
|
|---|
| 166 | *
|
|---|
| 167 | * const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector;
|
|---|
| 168 | */
|
|---|
| 169 | export interface TypedUseSelectorHook<TState> {
|
|---|
| 170 | <TSelected>(
|
|---|
| 171 | selector: (state: TState) => TSelected,
|
|---|
| 172 | equalityFn?: EqualityFn<NoInfer<TSelected>>,
|
|---|
| 173 | ): TSelected
|
|---|
| 174 | <Selected = unknown>(
|
|---|
| 175 | selector: (state: TState) => Selected,
|
|---|
| 176 | options?: UseSelectorOptions<Selected>,
|
|---|
| 177 | ): Selected
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | export type NoInfer<T> = [T][T extends any ? 0 : never]
|
|---|