【发布时间】:2021-12-14 22:25:12
【问题描述】:
我正在开发一个组件,该组件接收一个 children 道具,该道具接收一个 img 元素 + 另一个道具。但是当我继续使用该组件并传递所有道具时,我收到以下错误:
Type 'Element' has no properties in common with type 'Pick<ImgHTMLAttributes<HTMLImageElement>, "src" | "alt">'.ts(2559)
App.tsx(5, 3): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & Props'
这是我的代码示例 (Link to CodeSandbox):
type Props = {
children: Pick<ImgHTMLAttributes<HTMLImageElement>, "src" | "alt">
otherProp?: any
}
const Image = ({children, otherProp}: Props) => {
return (
<picture>
...
{children}
</picture>
)
}
老实说,我不明白IntrinsicAttributes 是什么,我可以看到它正在扩展我的Prop 与其他类型的IntrinsicAttributes,但不知道为什么。当我删除otherProp 时,错误似乎也消失了。一个解释会非常有帮助。
非常感谢。
【问题讨论】:
标签: reactjs typescript typeerror