使用ownProps /打字稿

时间:2019-02-07 13:43:11

标签: reactjs typescript redux react-redux

当redux connect和选项一起被调用但没有OwnProps时,它将导致有关内置道具(如ref)的打字稿问题。所以,如果我尝试:

父组件:

import MyComponent from './MyComponent';

class ParentComponent extends React.Component<Props, {}> {
  private placeholder: any;

  constructor(props) {
    super(props);
  }
  render() {
    <MyComponent ref={(e: any) => { this.placeholder = e; }} />
  }
}

我的组件

import { doSomethingAction } from 'actions';

interface State { something?: string; }
interface StateProps { someProp: string; }
interface DispachProps { doSomething: () => void }
type Props = StateProps & DispatchProps;

class MyComponent<Props, State> {
  constructor(props) {
    super(props);
    this.state: State = { someProp: false };
  }
  render() {
    return (<div>this.props.someProp</div>);
  }
}

function mapStateToProps(state: Store): StateProps {
  return {
    someProp: state.somePropState
  }
}

function mapDispachToProps(dispach): DispachProps {
  return {
    doSomething: () => dispach(doSomethingAction())
  }
}

// same issue even if third type for OwnProps is added
export default connect<StateProps, DispachProps> (
  mapStateToProps,
  mapDispachToProps,
  null,
  { forwardRef: true }
)(MyComponent)

我的MyComponent出现以下打字错误:

输入'{ref:(e:any)=> void; }”缺少类型“ Readonly>”的以下属性:someProp,doSomething。

突然,它将所有这些属性视为必需的自身属性。即使定期为OwnProps添加第三种类型,也会产生相同的错误。那是一个错误还是我错过了一些东西。如果它是一个错误,那么有什么优雅的方法可以解决这个问题,直到它被修复(我知道我可以为OwnProps添加第三种类型,但是认为它不是很好的解决方案)? (反应16.8.0,react-redux 6.0.0,@ types /反应16.8.2,@ types / react-redux 7.0.1)

0 个答案:

没有答案
相关问题