简体   繁体   English

Typescript - 如何修复非 Typescript React 组件的 children 属性上的“属性子项不存在”错误?

[英]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.

相关问题 "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' TypeScript 错误:属性“children”在类型“ReactNode”上不存在 - TypeScript error: Property 'children' does not exist on type 'ReactNode' TypeScript 和 React 18 中的儿童道具 - Children prop in TypeScript and React 18 如何使用反应打字稿修复“{}”上不存在的属性? - How to fix property does not exist on '{}' with react typescript? 重构对Typecript的反应会导致错误属性&#39;readOnly&#39;在类型&#39;IntrinsicAttributes&InputProps&{children?:ReactNode; }” - Refactoring React to Typescript causes error Property 'readOnly' does not exist on type 'IntrinsicAttributes & InputProps & { children?: ReactNode; }' 使用 Typescript 将 React children 属性限制为每个给定类型的一个组件 - Limiting React children prop to one component of each given type with Typescript Typescript:如何在React Component中设置子类型? - Typescript: how to set type of children in React Component? 如何禁止孩子使用 typescript 的 React 组件? - How to forbid children for a React component with typescript? 使用打字稿对组件子项进行类型检查 - React Component children typecheck with typescript React 组件的索引子组件为 TypeScript - Index children of React component with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM