有没有人能帮我解决我遇到的打字问题?
在我的项目中运行tsc时遇到以下错误:
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中使用它的位置状态的代码行中指出了错误。
基本上,我的项目中的代码是这样的:
[...]
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的版本如下:
[...]
"dependencies": {
[...]
"react-router": "5.2.0",
"react-router-dom": "^5.2.0",
[...]
"devDependencies": {
"@types/react-router-dom": "^5.1.7",
[...]发布于 2021-11-16 20:24:17
也许你打算将item作为state来传递(第二个参数):
history.push(
{
pathname: `/orders/${orderId}/items/${row.id}`,
},
{ item: row }
);https://stackoverflow.com/questions/69995360
复制相似问题