首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'{ pathname: string;shipment: TShipment;}‘类型的参数不能赋值给' to’类型的参数

'{ pathname: string;shipment: TShipment;}‘类型的参数不能赋值给' to’类型的参数
EN

Stack Overflow用户
提问于 2021-11-16 19:57:49
回答 1查看 35关注 0票数 0

有没有人能帮我解决我遇到的打字问题?

在我的项目中运行tsc时遇到以下错误:

代码语言:javascript
复制
Argument of type '{ pathname: string; item: Item; }' is not assignable to parameter of type 'To'.
Object literal may only specify known properties, and 'item' does not exist in type 'PartialPath'.

在我试图在history.push中使用它的位置状态的代码行中指出了错误。

基本上,我的项目中的代码是这样的:

代码语言:javascript
复制
[...]

interface RouterParams {
  orderId: string,
}
interface LocationState {
  item: Item,
}
interface RowProps extends RouteComponentProps<RouterParams, StaticContext, LocationState> {
  item: Item,
};

const Row = ({ row, history, match }: RowProps) => (
  <button
   onClick={
      const orderId = match.params.orderId;
      history.push({
        pathname: `/orders/${orderId}/items/${row.id}`,
        // Here is where tsc indicates the error
        item: row,
      });
   }
  >
  </button>
);

export default withRouter(Row);

我在package.json中使用的react-router和types的版本如下:

代码语言:javascript
复制
[...]
"dependencies": {
[...]
  "react-router": "5.2.0",
  "react-router-dom": "^5.2.0",
[...]
"devDependencies": {
  "@types/react-router-dom": "^5.1.7",
[...]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-16 20:24:17

也许你打算将item作为state来传递(第二个参数):

代码语言:javascript
复制
history.push(
  {
    pathname: `/orders/${orderId}/items/${row.id}`,
  },
  { item: row }
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69995360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档