【发布时间】:2020-04-18 15:35:42
【问题描述】:
我正在将一个使用 Material-ui 的 js 组件转换为 typescript,但遇到了问题。这部分正在渲染一个类似平铺的图像,其中 component 属性被覆盖,加上额外的 to 属性以创建正确的可点击链接。
我得到的错误是: TS2769:没有重载匹配这个调用。
这是我正在使用的代码:
进口声明:
import GridListTile from '@material-ui/core/GridListTile';
import { Link } from 'react-router-dom';
在渲染功能中我有:
<GridListTile
component={Link} <<-- here I get the error
to={'/some-address'}
>
// other components
</GridListTile>
我可以通过传递 as any 来消除错误,但随后它会抱怨 to 道具!
<GridListTile
component={Link as any} <<-- won't complain
to={'/some-address'} <<-- but now here I get the error
>
// other components
</GridListTile>
我希望能够使用 Material-ui 组件并能够传递另一个组件和我想要的道具。 我遇到了一些黑客解决方案,处理这种情况的正确方法是什么?
【问题讨论】:
-
无法重现您遇到的错误。你能提供一个在线演示吗? codesandbox.io/s/pedantic-sea-edbt1?file=/src/App.tsx
-
@keikai 这是一个沙箱,它只在
toprop 上显示警告,但我的应用程序会抛出错误! codesandbox.io/s/typescript-react-material-ui-cq8e1?file=/src/… -
仍然找不到你提到的错误
TS2769: No overload matches this call.。顺便说一句,react-router-dom的ts类型需要@types/react-router-dom
标签: javascript reactjs typescript