【问题标题】:React Redux connect is not assignable to type '() => void' with typescriptReact Redux 连接不可分配给使用打字稿键入 '() => void'
【发布时间】:2019-10-07 15:01:49
【问题描述】:

一切都好吗?

我在理解问题时感到困惑,使用带有反应的键入文本,当连接或使用组件还原时,它会出现此错误并且无法理解,做一些研究,但答案很混乱

错误:

Argument of type 'typeof Beds' is not assignable to parameter of type 'ComponentType<Matching<{ beds: DataBedsTypes[]; } & typeof import("/Users/keven/Documents/carenet/orquestra-frontend/src/Beds/action"), Props>>'.
  Type 'typeof Beds' is not assignable to type 'ComponentClass<Matching<{ beds: DataBedsTypes[]; } & typeof import("/Users/keven/Documents/carenet/orquestra-frontend/src/Beds/action"), Props>, any>'.
    Types of parameters 'props' and 'props' are incompatible.
      Type 'Matching<{ beds: DataBedsTypes[]; } & typeof import("/Users/keven/Documents/carenet/orquestra-frontend/src/Beds/action"), Props>' is not assignable to type 'Readonly<Props>'.
        Types of property 'loadBeds' are incompatible.
          Type '(data: DataBedsTypes[]) => { type: BedsTypes; payload: DataBedsTypes[]; }' is not assignable to type '() => void'.  TS2345

    46 |   mapStateToProps,
    47 |   mapDispatchToProps,
  > 48 | )(Beds);

我的组件

import React, { Component } from 'react';

import { connect } from 'react-redux';
import { bindActionCreators, Dispatch } from 'redux';
import { Row, Col } from 'config/styles';
import { ApplicationState } from 'config/store';
import { DataBedsTypes } from './types';
import * as BedsActions from './action';

import Bed from './Bed';

interface StateProps {
  beds: DataBedsTypes[];
}

interface DispatchProps {
  loadBeds(): void;
}

type Props = StateProps & DispatchProps;

class Beds extends Component<Props> {
  componentWillMount() {
    const { loadBeds } = this.props;
    loadBeds();
  }

  render() {
    const { beds } = this.props;
    return (
      <Row>
        {beds.map((b: DataBedsTypes) => (
          <Col key={b.pid} md={16.666} lg={10}>
            <Bed {...b} />
          </Col>
        ))}
      </Row>
    );
  }
}

const mapStateToProps = (state: ApplicationState) => ({ beds: state.beds.data });
const mapDispatchToProps = (dispatch: Dispatch) => bindActionCreators(BedsActions, dispatch);

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Beds);

【问题讨论】:

    标签: reactjs typescript redux


    【解决方案1】:

    您的 DispatchProps 接口错误。现在你说 loadBeds 是 void 类型。你的界面应该是这样的

    interface DispatchProps {
      loadBeds: () => void;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2021-08-19
      • 2018-04-16
      • 1970-01-01
      • 2018-01-04
      • 2017-09-10
      • 2018-09-29
      • 2021-12-29
      • 2016-11-09
      相关资源
      最近更新 更多