diff options
Diffstat (limited to 'sdnr/wt/odlux/framework/src/flux/connect.ts')
-rw-r--r-- | sdnr/wt/odlux/framework/src/flux/connect.ts | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/sdnr/wt/odlux/framework/src/flux/connect.ts b/sdnr/wt/odlux/framework/src/flux/connect.ts index fca7df6fa..f54e4e0f0 100644 --- a/sdnr/wt/odlux/framework/src/flux/connect.ts +++ b/sdnr/wt/odlux/framework/src/flux/connect.ts @@ -38,19 +38,15 @@ interface IDispatchProps { dispatch: Dispatch; } -type FuncInfer<T> = { - ([...args]: any): T; -}; - -type FunctionResult<T> = T extends FuncInfer<infer U> ? U : never; +type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> type ComponentDecoratorInfer<TMergedProps> = { - <TProps>(wrappedComponent: React.ComponentType<TProps & TMergedProps>): React.ComponentClass<TProps>; + <TProps>(wrappedComponent: React.ComponentType<TProps & TMergedProps>): React.ComponentClass<Omit<TProps & TMergedProps, keyof TMergedProps>>; }; -export type Connect<TMapProps = undefined, TMapDispatch = undefined> = - (TMapProps extends undefined ? IApplicationStoreProps : FunctionResult<TMapProps>) & - (TMapDispatch extends undefined ? IDispatchProps : FunctionResult<TMapDispatch>); +export type Connect<TMapProps extends ((...args: any) => any) | undefined = undefined, TMapDispatch extends ((...args: any) => any) | undefined = undefined> = + (TMapProps extends ((...args: any) => any) ? ReturnType<TMapProps> : IApplicationStoreProps) & + (TMapDispatch extends ((...args: any) => any) ? ReturnType<TMapDispatch> : IDispatchProps); export function connect(): ComponentDecoratorInfer<IApplicationStoreProps & IDispatchProps>; @@ -73,9 +69,9 @@ export function connect<TDispatchProps>( export function connect<TProps, TStateProps, TDispatchProps>( mapStateToProps?: ((state: IApplicationStoreState) => TStateProps), mapDispatchToProps?: ((dispatcher: IDispatcher) => TDispatchProps) -) : +): ((WrappedComponent: React.ComponentType<TProps & (IApplicationStoreProps | TStateProps) & IDispatchProps>) => React.ComponentType<TProps>) { - + const injectApplicationStore = (WrappedComponent: React.ComponentType<TProps & (IApplicationStoreProps | TStateProps) & IDispatchProps>): React.ComponentType<TProps> => { class StoreAdapter extends React.Component<TProps, {}> { @@ -83,7 +79,7 @@ export function connect<TProps, TStateProps, TDispatchProps>( context: IApplicationStoreContext; render(): JSX.Element { - + if (isWrappedComponentIsVersion1(WrappedComponent)) { const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, dispatch: this.store.dispatch.bind(this.store) }); return element; @@ -91,10 +87,10 @@ export function connect<TProps, TStateProps, TDispatchProps>( const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), dispatch: this.store.dispatch.bind(this.store) }); return element; } else if (mapStateToProps && mapDispatchToProps && isWrappedComponentIsVersion3(WrappedComponent)) { - const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store)}) as any) }); + const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store) }) as any) }); return element; } else if (!mapStateToProps && mapDispatchToProps && isWrappedComponentIsVersion4(WrappedComponent)) { - const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store)}) as any) }); + const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store) }) as any) }); return element; } throw new Error("Invalid arguments in connect."); @@ -158,7 +154,7 @@ export class ApplicationStoreProvider extends React.Component<ApplicationStorePr } render(): JSX.Element { - return React.Children.only(this.props.children); + return React.Children.only(this.props.children) as any; //type error, fix when possible } } |