【问题标题】:compose<any> how sould I type this stuff?compose<any> 我是怎么输入这些东西的?
【发布时间】:2020-06-21 12:59:29
【问题描述】:

我有问题要问你!我在 typeScript 上迁移了我的整个应用程序,但在 compose 功能上遇到了一些困难......(不用担心我已经修复的其他任何问题,除了 compose())

使用compose(...)(没有“任何”)我得到以下信息:

没有重载匹配这个调用。 Overload 1 of 2, '(props: Readonly): Route',给出了以下错误。 类型“未知”不可分配给类型“ComponentClass |功能组件 |组件类,任意> |函数组件<...> |不明确的'。 类型“未知”不可分配给类型“FunctionComponent>”。 Overload 2 of 2, '(props: RouteProps, context?: any): Route',给出了以下错误。 类型“未知”不可分配给类型“ComponentClass |功能组件 |组件类,任意> |函数组件<...> |不明确的'。 类型“未知”不可分配给类型“FunctionComponent>”。 TS2769

这就是为什么我想替换它并输入它。

这是我的代码:

/container.ts

//@ts-ignore
import { connect } from "react-redux";
import { compose, AnyAction, Dispatch } from "redux";
import { withRouter } from "react-router-dom";
import { getProfile, updateProfile } from "../../../services/clients/user";
import { updateUserInitial } from "../../../actions/initialUser";
import { IStoreState } from "../../typeScriptInclude";
import { ThunkDispatch } from "redux-thunk";
import { Profile } from "./component/utilities/includeTypeScript";
import { IEditAccountProps as Props } from './component/EditAccount';

interface IupDateWalterInitailUserPropsType {
  walterUser: {
    first_name: string;
    last_name: string;
  };
}

interface IMapStateToProps{
  isConnect: boolean;
  walterUser: any;
  upDateProfile: any;
}

interface IMapDispatchToProps{
  upDateWalterInitailUser: any;
}

const mapStateToProps = (state: IStoreState): IMapStateToProps => {
  return {
    isConnect: state.isConnect,
    walterUser: getProfile(),
    upDateProfile: (data: Profile) => updateProfile(data)
  };
};

type ApplicationDispatch = ThunkDispatch<IStoreState, void, AnyAction> &
  Dispatch;

const mapDispatchToProps = (
  dispatch: ApplicationDispatch,
): IMapDispatchToProps => ({
  upDateWalterInitailUser: (form: IupDateWalterInitailUserPropsType) =>
    dispatch(updateUserInitial(form))
});

export default compose<any>(
  withRouter,
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
);

/index.ts

import EditAccount from "./component/EditAccount";
import container from './container';

export default container(EditAccount);

事情是我尝试了很多东西来输入它,但无能为力...... 我试过了

// Which props define our new component : IProps - IInjectedProps
type ContainerProps = Omit<Props, keyof InjectedProps>;

export default compose<Props, ContainerProps>(
  withRouter,
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
);

我起飞 withRouter 但我得到以下信息:

预期 2 个参数,但得到 1 个。TS2554

显然,但为什么它不适用于我的第一个解决方案......需要了解

谢谢大家最好的问候。

【问题讨论】:

  • 我认为您应该将组合功能应用于组件。你想在这里connect 什么组件?
  • 我在帖子中添加了更多详细信息;)但要> EditAccount
  • 您在执行compose(...) 时是否遇到语法错误?如果是这样,错误是什么?如果不是,为什么要输入它?
  • 对不起,我一开始不明白你的意思,但我明白了:No overload matches this call. Overload 1 of 2,... Type 'unknown' is not assignable to type 'ComponentClass&lt;any, any&gt; | FunctionComponent&lt;any&gt; | ComponentClass&lt;RouteComponentProps&lt;any, StaticContext, PoorMansUnknown&gt;, any&gt; | FunctionComponent&lt;...&gt; | undefined'. Type 'unknown' is not assignable to type 'FunctionComponent&lt;RouteComponentProps&lt;any, StaticContext, PoorMansUnknown&gt;&gt;'. TS2769

标签: reactjs typescript redux


【解决方案1】:

这是connect 的问题,而不是compose。我记得,node_modules/@types 中的react 版本不匹配。 react-redux types 文件夹中有它自己的 node_modules 并从中读取 react 类型。它与根node_modules 中的实际react 文件夹不同。所以,发生的情况是,ComponentClass 类型是从两个不同的 .d.ts 文件中读取的,并且不匹配,因此出现错误。

您可以 @ts-ignore connect 行并忘记它。这对我来说不是很吸引人,所以我所做的(虽然这是一个 hack 并且有点整洁)是我从 root/node_modules/@types/react-nativeroot/node_modules/@types/react-redux 中删除了 node_modules。这样做,使tslint 能够从root/node_modules/@types/react 的同一文件中读取类型。它解决了问题,但每次安装或卸载软件包时,node_modules 会再次出现。您每次都必须删除它们(我有一个 cmd 批处理文件)。希望这可以帮助。

【讨论】:

  • 我真的不明白你的意思,除了它是一个模块问题:/ 但据我所知,我需要删除我的模块并重新安装?
  • 不,你只需要删除root/node_modules/@types/react-native/node_modulesroot/node_modules/@types/react-redux/node_modules。您可以导航到这些文件夹并亲自查看。里面有 react types 模块,它是一个旧版本。
  • okok 我做到了,但如果我必须按照你之前所说的那样每次都这样做,那就太麻烦了,而且是一种黑客行为......所以没有合法的东西可以解决它?因为如果我部署它,它可能随时崩溃......
  • 嗯,到目前为止我还没有找到它。我们必须等到这些库同步并添加正确的react 类型版本。您可以创建一个批处理文件并在安装后使用一个命令完成所有操作。如果你愿意,我可以给你批处理文件作为答案的编辑。
  • 我相信它应该。正如我所说,您的错误来自connect 的输入,您将不再需要它。据我所知,redux 钩子没有这样的问题。此外,如果您改用钩子,您将需要更少的代码和维护。
猜你喜欢
  • 1970-01-01
  • 2019-11-24
  • 2012-12-28
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多