我有一个除了 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/