TypeScript struggles with Redux containers

后端 未结 3 1150
生来不讨喜
生来不讨喜 2020-12-29 06:05

I\'m having some trouble figuring out how to properly type Redux containers.

Consider a simple presentational component that might look like this:

in         


        
3条回答
  •  清酒与你
    2020-12-29 06:52

    interface MyStateProps {
        name: string;
        selected: boolean;
    }
    
    interface MyDispatchProps {
        onSelect: (name: string) => void;
    }
    
    interface MyOwnProps {
        section: string;
    }
    
    // Intersection Types
    type MyProps = MyStateProps & MyDispatchProps & MyOwnProps;
    
    
    class MyComponent extends React.Component { }
    
    function mapStateToProps(state: MyState): MyStateProps { }
    
    function mapDispatchToProps(dispatch: IDispatch): MyDispatchProps { }
    
    const MyContainer = connect(
      mapStateToProps,
      mapDispatchToProps
    )(MyComponent);
    

    You can use something use called Intersection Types https://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types

提交回复
热议问题