Last active
January 5, 2018 15:38
-
-
Save blindbox/784c8c4e68c9f1252bd4dfa1f2f56404 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
// typeTools.ts | |
// https://stackoverflow.com/a/46944148 | |
interface Func<T> { | |
([...args]: any, args2?: any): T // tslint:disable-line | |
} | |
export function returnType<T>(func: Func<T>): T { | |
return (false as any) as T | |
} | |
// end typeTools.ts | |
interface ExposedProps { // props that you pass in from the parent component | |
someExposedProp: SomeOtherType, | |
} | |
const mapStateToProps = (state: State): ReduxStates => { | |
return {someState: someStateSelector(state)} | |
} | |
const mapDispatchToProps = { // short-hand dispatch syntax ftw | |
someDispatcher, | |
} | |
const mapStateToPropsTypeMaker = returnType(mapStateToProps) | |
type ReduxStates = typeof mapStateToPropsTypeMaker | |
type Props = ExposedProps & typeof mapDispatchToProps & ReduxStates | |
class ConnectedComponentBase extends React.Component<Props> {} | |
export const ConnectedComponent = connect(mapStateToProps, mapDispatchToProps)(ConnectedComponentBase) as any as React.ComponentClass<ExposedProps> // any is required to silence all complaints about incompatible types. It makes sense, ConnectedComponentBase might contain props that exceeds what's available for ExposedProps. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would be great to have a detailed blog post about this.