【发布时间】:2023-03-26 18:21:01
【问题描述】:
我有一个包装 Router 的组件并采用额外的道具。我尝试以类似于this cheatsheet 的方式使用PropsWithoutRef:
import React, { ComponentType, PropsWithoutRef } from 'react'
import { Route } from 'react-router'
type LayoutRouteProps = PropsWithoutRef<Route> & {
component: ComponentType,
};
const DashboardLayoutRoute = ({
component: Component,
...rest
}: LayoutRouteProps) => {
const render = (props: any)=> <Component {...props} />
return <Route {...rest} render={render} />
};
但是没有用。当我尝试使用DashboardLayoutRoute 并传递与Route 相关的道具时:
<DashboardLayoutRoute exact path='/' component={Home} />
我明白了
错误:(63, 39) TS2322: 类型 '{ 精确: true;路径:字符串;成分:任何; }' 不可分配给类型 'IntrinsicAttributes & Route & { component: ComponentType; }'。
类型'IntrinsicAttributes & Route & { component: ComponentType 上不存在属性'exact'; }'。
错误:(66, 39) TS2322: Type '{ path: string;成分:任何; }' 不可分配给类型 'IntrinsicAttributes & Route & { component: ComponentType; }'。
类型'IntrinsicAttributes & Route & { component: ComponentType 上不存在属性'path'; }'。
我的代码有什么问题?
【问题讨论】:
标签: reactjs typescript