reactjs - React-Redux connect() 和错误 TS2345

标签 reactjs typescript react-redux

我无法弄清楚为什么此代码无法构建并出现以下错误:

(43,3): error TS2345: Argument of type 'typeof Day' is not assignable to parameter of type 'Component<Props | (Props & ConnectProps & DispatchProps)>'.
  Type 'typeof Day' is not assignable to type 'StatelessComponent<Props | (Props & ConnectProps & DispatchProps)>'.
    Type 'typeof Day' provides no match for the signature '(props: (Props & { children?: ReactNode; }) | (Props & ConnectProps & DispatchProps & { children?: ReactNode; }), context?: any): ReactElement<any>'.

我运行的是 typescript 2.3.3,@types/react-redux 的版本是 4.4.41。

import * as React from "react";
import { connect } from "react-redux";

export interface Props {
  date: Date;
}

export interface ConnectProps {
  shifts: string[];
}

export interface DispatchProps {}

class Day extends React.Component<Props & ConnectProps & DispatchProps, null> {
  render() {
    /* simplified */
    return <div />;
  }
}

const mapStateToProps = (state: any, ownProps?: Props): ConnectProps => {
  /* simplified */
  return {
    shifts: []
  };
};

const mapDispatchToProps = (dispatch: any): DispatchProps => ({});

export default connect<ConnectProps, DispatchProps, Props>(
  mapStateToProps,
  mapDispatchToProps
)(Day);

let component = <Day date={new Date()} />

我想避免装饰器,只要它们是实验性的。

最佳答案

I want to avoid decorators as long as they are experimental.

处理方法错误。它们的工作方式就像 JSX 的工作方式。当前的类型定义假设您将使用装饰器。

更多

如果您仍然不想使用装饰器,则必须编写自己的类型定义,然后覆盖 redux 中的类型定义,这两者都不是简单的挑战。欢迎您出于乐趣来解决这些问题,但需要的时间比有用SO答案中所能提供的时间要多。

关于reactjs - React-Redux connect() 和错误 TS2345,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44370696/

相关文章:

javascript - 状态未使用 React redux thunk 更新

reactjs - 如何使用传播函数删除React中的嵌套对象

reactjs - 调度 Redux 操作是否被认为是昂贵的?

reactjs - 如何使用 gorilla/mux 在 GO 中将我的 bundle.js 文件与我的 html 一起交付

javascript - React - useState 和 Firebase 的问题

javascript - 嵌套在 ngIf 中时,异步管道订阅无法正常工作

javascript - 在既是值访问器又是函数的对象上定义属性

angular - 公共(public)或私有(private) - Angular 2 组件类方法混淆

javascript - 如何在react中使用同步调用

reactjs - 在具有处理表单式事件和行为的 HOC 输入标记的 ReactJS 中是否需要使用 <form> 标记?