[英]Typescript - how do I fix "Property children does not exist" error on the children prop of a non-Typescript React component?
I have a parent component that is written in the tsx
extension and a child component that is written in the 'jsx' extension:我有一个用
tsx
扩展编写的父组件和一个用“jsx”扩展编写的子组件:
Child.jsx (not the actual component but just an example) Child.jsx (不是实际的组件,只是一个示例)
const Child = props => {
const { children } = props
return (<a>{children}</a>)
}
Parent.tsx父.tsx
const Parent = (props: IProps) => {
return (
<Child>This is my data</Child>
)
}
However, I get a Typescript error on the Parent.tsx
:但是,我在 Parent.tsx 上收到
Parent.tsx
错误:
Property 'children' does not exist on type 'IntrinsicAttributes
类型“IntrinsicAttributes”上不存在属性“子项”
Now, in a perfect world, I might refactor the Child.jsx
to be a Typescript file and type the children
prop as React.ReactNode
but what if this is a legacy Child file that is incredibly complex and itself has children that need to refactored, etc.?现在,在一个完美的世界中,我可能会将
Child.jsx
重构为 Typescript 文件并将children
属性键入为React.ReactNode
但如果这是一个非常复杂且本身具有需要重构的子文件的遗留子文件怎么办, ETC。?
Is there any way to get over the Typescript error being emitted by the Parent?有什么办法可以克服 Parent 发出的 Typescript 错误?
React 18 stopped including implicit children prop. React 18 停止包含隐式 children 道具。 You need to add it explicitly.
您需要明确添加它。
https://solverfox.dev/writing/no-implicit-children/ https://solverfox.dev/writing/no-implicit-children/
We need to start submitting PRs to the various dependency projects (as I have done twice so far) to add the explicit children prop.我们需要开始向各种依赖项目提交 PR(到目前为止我已经做了两次)以添加显式的 children 属性。 Perhaps downgrade React version until that situation calms down?
也许降级 React 版本直到这种情况平静下来?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.