javascript - React.PropsWithChildren 除了 `children` 之外没有任何 Prop ?

标签 javascript reactjs typescript

我有一个除了 children 之外没有任何 Prop 的组件, IE。:

function Foo({ children }: React.PropsWithChildren<>) {}
React.PropsWithChildren需要一个论据。如果我这样做 React.PropsWithChildren<{}> , Eslint 生产 Don't use `{}` as a type. `{}` actually means "any non-nullish value". .
我将它用于空对象类型:
type EmptyObj = { [k: string]: never };
但是,React.PropsWithChildren<EmptyObj>原因:
Type '{ children: Element; }' is not assignable to type 'EmptyObj'.
  Property 'children' is incompatible with index signature.
    Type 'Element' is not assignable to type 'never'.
我应该把什么传给 React.PropsWithChildren ?
编辑:
我知道我可以这样做:
function Foo({ children }: { children?: React.ReactNode }) {}
但是,我正在使用 React.PropsWithChildren在整个代码库中,所以我更愿意保持一致。

最佳答案

解决方案是:

function Foo({ children }: React.PropsWithChildren<Record<never, any>>) {}
如果 eslint 警告你关于类型 any使用 Record<never, never>反而。

关于javascript - React.PropsWithChildren 除了 `children` 之外没有任何 Prop ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65548388/

相关文章:

Typescript:推断嵌套 keyof 属性的类型

javascript - 使用Jquery删除div之间的div

javascript - 如何在 React 中的 GraphQL 突变组件中包含的按钮上有多个 onClick 事件

typescript - 从 typescript 到 Dart 书写转译器时,人们面临哪些技术障碍?

javascript - TemplateUrl 找不到我的模板

node.js - React npm run build,那是什么,为什么我们需要它?

javascript - SAPUI5:仅设置可滚动表的元素

javascript - `=` 和 `==` 运算符之间有什么区别,什么是 `===` ? (单、双、三等号)

javascript - ionic 存储实现: get and set

javascript - 运行简单的 Electron 程序时出错