Skip to content

Commit 152d456

Browse files
committed
Copy most standalone types from @types/react-redux
1 parent 6f94ced commit 152d456

File tree

1 file changed

+257
-0
lines changed

1 file changed

+257
-0
lines changed

src/types.ts

+257
Original file line numberDiff line numberDiff line change
@@ -1 +1,258 @@
1+
/* eslint-disable no-unused-vars */
2+
// TODO Ignoring all unused variables for now
3+
4+
import {
5+
ClassAttributes,
6+
Component,
7+
ComponentClass,
8+
ComponentType,
9+
StatelessComponent,
10+
Context,
11+
NamedExoticComponent,
12+
} from 'react'
13+
14+
import { Action, ActionCreator, AnyAction, Dispatch, Store } from 'redux'
15+
16+
// import hoistNonReactStatics = require('hoist-non-react-statics');
17+
import type { NonReactStatics } from 'hoist-non-react-statics'
18+
119
export type FixTypeLater = any
20+
21+
/**
22+
* This interface can be augmented by users to add default types for the root state when
23+
* using `react-redux`.
24+
* Use module augmentation to append your own type definition in a your_custom_type.d.ts file.
25+
* https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
26+
*/
27+
// tslint:disable-next-line:no-empty-interface
28+
export interface DefaultRootState {}
29+
30+
export type AnyIfEmpty<T extends object> = keyof T extends never ? any : T
31+
export type RootStateOrAny = AnyIfEmpty<DefaultRootState>
32+
33+
// Omit taken from https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
34+
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
35+
36+
export type DistributiveOmit<T, K extends keyof T> = T extends unknown
37+
? Omit<T, K>
38+
: never
39+
40+
export interface DispatchProp<A extends Action = AnyAction> {
41+
dispatch: Dispatch<A>
42+
}
43+
44+
export type AdvancedComponentDecorator<TProps, TOwnProps> = (
45+
component: ComponentType<TProps>
46+
) => NamedExoticComponent<TOwnProps>
47+
48+
/**
49+
* A property P will be present if:
50+
* - it is present in DecorationTargetProps
51+
*
52+
* Its value will be dependent on the following conditions
53+
* - if property P is present in InjectedProps and its definition extends the definition
54+
* in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
55+
* - if property P is not present in InjectedProps then its definition will be that of
56+
* DecorationTargetProps[P]
57+
* - if property P is present in InjectedProps but does not extend the
58+
* DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
59+
*/
60+
export type Matching<InjectedProps, DecorationTargetProps> = {
61+
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
62+
? InjectedProps[P] extends DecorationTargetProps[P]
63+
? DecorationTargetProps[P]
64+
: InjectedProps[P]
65+
: DecorationTargetProps[P]
66+
}
67+
68+
/**
69+
* a property P will be present if :
70+
* - it is present in both DecorationTargetProps and InjectedProps
71+
* - InjectedProps[P] can satisfy DecorationTargetProps[P]
72+
* ie: decorated component can accept more types than decorator is injecting
73+
*
74+
* For decoration, inject props or ownProps are all optionally
75+
* required by the decorated (right hand side) component.
76+
* But any property required by the decorated component must be satisfied by the injected property.
77+
*/
78+
export type Shared<InjectedProps, DecorationTargetProps> = {
79+
[P in Extract<
80+
keyof InjectedProps,
81+
keyof DecorationTargetProps
82+
>]?: InjectedProps[P] extends DecorationTargetProps[P]
83+
? DecorationTargetProps[P]
84+
: never
85+
}
86+
87+
// Infers prop type from component C
88+
export type GetProps<C> = C extends ComponentType<infer P>
89+
? C extends ComponentClass<P>
90+
? ClassAttributes<InstanceType<C>> & P
91+
: P
92+
: never
93+
94+
// Applies LibraryManagedAttributes (proper handling of defaultProps
95+
// and propTypes), as well as defines WrappedComponent.
96+
export type ConnectedComponent<
97+
C extends ComponentType<any>,
98+
P
99+
> = NamedExoticComponent<JSX.LibraryManagedAttributes<C, P>> &
100+
NonReactStatics<C> & {
101+
WrappedComponent: C
102+
}
103+
104+
// Injects props and removes them from the prop requirements.
105+
// Will not pass through the injected props if they are passed in during
106+
// render. Also adds new prop requirements from TNeedsProps.
107+
// Uses distributive omit to preserve discriminated unions part of original prop type
108+
export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> = <
109+
C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>
110+
>(
111+
component: C
112+
) => ConnectedComponent<
113+
C,
114+
DistributiveOmit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> &
115+
TNeedsProps
116+
>
117+
118+
// Injects props and removes them from the prop requirements.
119+
// Will not pass through the injected props if they are passed in during
120+
// render.
121+
export type InferableComponentEnhancer<
122+
TInjectedProps
123+
> = InferableComponentEnhancerWithProps<TInjectedProps, {}>
124+
125+
export type InferThunkActionCreatorType<
126+
TActionCreator extends (...args: any[]) => any
127+
> = TActionCreator extends (
128+
...args: infer TParams
129+
) => (...args: any[]) => infer TReturn
130+
? (...args: TParams) => TReturn
131+
: TActionCreator
132+
133+
export type HandleThunkActionCreator<TActionCreator> = TActionCreator extends (
134+
...args: any[]
135+
) => any
136+
? InferThunkActionCreatorType<TActionCreator>
137+
: TActionCreator
138+
139+
// redux-thunk middleware returns thunk's return value from dispatch call
140+
// https://github.com/reduxjs/redux-thunk#composition
141+
export type ResolveThunks<TDispatchProps> = TDispatchProps extends {
142+
[key: string]: any
143+
}
144+
? {
145+
[C in keyof TDispatchProps]: HandleThunkActionCreator<TDispatchProps[C]>
146+
}
147+
: TDispatchProps
148+
149+
// the conditional type is to support TypeScript 3.0, which does not support mapping over tuples and arrays;
150+
// once the typings are updated to at least TypeScript 3.1, a simple mapped type can replace this mess
151+
export type ResolveArrayThunks<
152+
TDispatchProps extends ReadonlyArray<any>
153+
> = TDispatchProps extends [
154+
infer A1,
155+
infer A2,
156+
infer A3,
157+
infer A4,
158+
infer A5,
159+
infer A6,
160+
infer A7,
161+
infer A8,
162+
infer A9
163+
]
164+
? [
165+
HandleThunkActionCreator<A1>,
166+
HandleThunkActionCreator<A2>,
167+
HandleThunkActionCreator<A3>,
168+
HandleThunkActionCreator<A4>,
169+
HandleThunkActionCreator<A5>,
170+
HandleThunkActionCreator<A6>,
171+
HandleThunkActionCreator<A7>,
172+
HandleThunkActionCreator<A8>,
173+
HandleThunkActionCreator<A9>
174+
]
175+
: TDispatchProps extends [
176+
infer A1,
177+
infer A2,
178+
infer A3,
179+
infer A4,
180+
infer A5,
181+
infer A6,
182+
infer A7,
183+
infer A8
184+
]
185+
? [
186+
HandleThunkActionCreator<A1>,
187+
HandleThunkActionCreator<A2>,
188+
HandleThunkActionCreator<A3>,
189+
HandleThunkActionCreator<A4>,
190+
HandleThunkActionCreator<A5>,
191+
HandleThunkActionCreator<A6>,
192+
HandleThunkActionCreator<A7>,
193+
HandleThunkActionCreator<A8>
194+
]
195+
: TDispatchProps extends [
196+
infer A1,
197+
infer A2,
198+
infer A3,
199+
infer A4,
200+
infer A5,
201+
infer A6,
202+
infer A7
203+
]
204+
? [
205+
HandleThunkActionCreator<A1>,
206+
HandleThunkActionCreator<A2>,
207+
HandleThunkActionCreator<A3>,
208+
HandleThunkActionCreator<A4>,
209+
HandleThunkActionCreator<A5>,
210+
HandleThunkActionCreator<A6>,
211+
HandleThunkActionCreator<A7>
212+
]
213+
: TDispatchProps extends [
214+
infer A1,
215+
infer A2,
216+
infer A3,
217+
infer A4,
218+
infer A5,
219+
infer A6
220+
]
221+
? [
222+
HandleThunkActionCreator<A1>,
223+
HandleThunkActionCreator<A2>,
224+
HandleThunkActionCreator<A3>,
225+
HandleThunkActionCreator<A4>,
226+
HandleThunkActionCreator<A5>,
227+
HandleThunkActionCreator<A6>
228+
]
229+
: TDispatchProps extends [infer A1, infer A2, infer A3, infer A4, infer A5]
230+
? [
231+
HandleThunkActionCreator<A1>,
232+
HandleThunkActionCreator<A2>,
233+
HandleThunkActionCreator<A3>,
234+
HandleThunkActionCreator<A4>,
235+
HandleThunkActionCreator<A5>
236+
]
237+
: TDispatchProps extends [infer A1, infer A2, infer A3, infer A4]
238+
? [
239+
HandleThunkActionCreator<A1>,
240+
HandleThunkActionCreator<A2>,
241+
HandleThunkActionCreator<A3>,
242+
HandleThunkActionCreator<A4>
243+
]
244+
: TDispatchProps extends [infer A1, infer A2, infer A3]
245+
? [
246+
HandleThunkActionCreator<A1>,
247+
HandleThunkActionCreator<A2>,
248+
HandleThunkActionCreator<A3>
249+
]
250+
: TDispatchProps extends [infer A1, infer A2]
251+
? [HandleThunkActionCreator<A1>, HandleThunkActionCreator<A2>]
252+
: TDispatchProps extends [infer A1]
253+
? [HandleThunkActionCreator<A1>]
254+
: TDispatchProps extends Array<infer A>
255+
? Array<HandleThunkActionCreator<A>>
256+
: TDispatchProps extends ReadonlyArray<infer A>
257+
? ReadonlyArray<HandleThunkActionCreator<A>>
258+
: never

0 commit comments

Comments
 (0)