-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.d.ts
89 lines (77 loc) · 3.58 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
declare module "@nozbe/with-observables" {
import { ComponentType, NamedExoticComponent } from "react";
import { Observable } from "rxjs";
import hoistNonReactStatics = require('hoist-non-react-statics');
export interface ObservableConvertible<T> {
readonly observe: () => Observable<T>;
}
export type ExtractObservableType<T> =
T extends Observable<infer U> ? U :
T extends ObservableConvertible<infer U> ? U :
T;
export type ExtractedObservables<T> = {
[K in keyof T]: ExtractObservableType<T[K]>
}
/**
* A property P will be present if:
* - it is present in DecorationTargetProps
*
* Its value will be dependent on the following conditions
* - if property P is present in InjectedProps and its definition extends the definition
* in DecorationTargetProps, then its definition will be that of DecorationTargetProps[P]
* - if property P is not present in InjectedProps then its definition will be that of
* DecorationTargetProps[P]
* - if property P is present in InjectedProps but does not extend the
* DecorationTargetProps[P] definition, its definition will be that of InjectedProps[P]
*/
type Matching<InjectedProps, DecorationTargetProps> = {
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
? InjectedProps[P] extends DecorationTargetProps[P]
? DecorationTargetProps[P]
: InjectedProps[P]
: DecorationTargetProps[P];
};
// Infers prop type from component C
export type GetProps<C> = C extends ComponentType<infer P> ? P : never;
/**
* a property P will be present if :
* - it is present in both DecorationTargetProps and InjectedProps
* - InjectedProps[P] can satisfy DecorationTargetProps[P]
* ie: decorated component can accept more types than decorator is injecting
*
* For decoration, inject props or ownProps are all optionally
* required by the decorated (right hand side) component.
* But any property required by the decorated component must be satisfied by the injected property.
*/
export type Shared<
InjectedProps,
DecorationTargetProps
> = {
[P in Extract<keyof InjectedProps, keyof DecorationTargetProps>]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never;
};
// Applies LibraryManagedAttributes (proper handling of defaultProps
// and propTypes), as well as defines WrappedComponent.
export type ConnectedComponent<
C extends ComponentType<any>,
P
> = NamedExoticComponent<JSX.LibraryManagedAttributes<C, P>> & hoistNonReactStatics.NonReactStatics<C> & {
WrappedComponent: C;
};
// Injects props and removes them from the prop requirements.
// Will not pass through the injected props if they are passed in during
// render. Also adds new prop requirements from TNeedsProps.
export type InferableComponentEnhancer<TInjectedProps, TNeedsProps> =
<C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
component: C
) => ConnectedComponent<C, Omit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> & TNeedsProps>;
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
export type ObservableifyProps<T, O extends keyof T, C extends keyof T = never> = {
[K in keyof Pick<T, O>]: Observable<T[K]>;
} & {
[K in keyof Pick<T, C>]: ObservableConvertible<T[K]>
} & Omit<T, O | C>
export default function withObservables<InputProps, ObservableProps>(
triggerProps: Array<keyof InputProps>,
getObservables: (props: InputProps) => ObservableProps
): InferableComponentEnhancer<ExtractedObservables<ObservableProps>, InputProps>
}