Typescript error when extending a forwardRef component e.g) ListItem.Placeholder = ListItemPlaceholder

huangapple go评论83阅读模式
英文:

Typescript error when extending a forwardRef component e.g) ListItem.Placeholder = ListItemPlaceholder

问题

我想将ListItemPlaceholder放在ListItem组件下面。ListItem在其中使用了forward ref。但是尽管我已经在文件末尾执行了ListItem.Placeholder = ListItemPlaceholder,但typescript仍然报错:

TS2322:类型'ForwardRefExoticComponent<Props & RefAttributes>'不能赋值给类型'ListItemComponent'。属性'Placeholder'在类型'ForwardRefExoticComponent<Props & RefAttributes>'中缺失,但在类型'{ Placeholder: FunctionComponent<{}>; }'中是必需的。

示例:

  1. type ListItemComponent = ForwardRefExoticComponent<Props> & {
  2. Placeholder: typeof ListItemPlaceholder;
  3. };
  4. export const ListItem: ListItemComponent = forwardRef<
  5. HTMLDivElement,
  6. Props
  7. >((props, ref) => {
  8. //.....
  9. return <div ref={ref}/>;
  10. })
  11. const ListItemPlaceholder: FunctionComponent = () => {
  12. return <div/>;
  13. };
  14. ListItem.Placeholder = ListItemPlaceholder;
英文:

I want to put the ListItemPlaceholder under the ListItem component. The ListItem uses forward ref in it. But the typescript complains about this even though I already do this: ListItem.Placeholder = ListItemPlaceholder at the end of the file.

TS2322: Type 'ForwardRefExoticComponent<Props & RefAttributes<HTMLDivElement>>' is not assignable to type 'ListItemComponent'.   Property 'Placeholder' is missing in type 'ForwardRefExoticComponent<Props & RefAttributes<HTMLDivElement>>' but required in type '{ Placeholder: FunctionComponent<{}>; }'.

Example:

  1. type ListItemComponent = ForwardRefExoticComponent&lt;Props&gt; &amp; {
  2. Placeholder: typeof ListItemPlaceholder;
  3. };
  4. export const ListItem: ListItemComponent = forwardRef&lt;
  5. HTMLDivElement,
  6. Props
  7. &gt;((props, ref) =&gt; {
  8. //.....
  9. return &lt;div ref={ref}/&gt;
  10. }))
  11. const ListItemPlaceholder: FunctionComponent = () =&gt; {
  12. return &lt;div/&gt;
  13. };
  14. ListItem.Placeholder = ListItemPlaceholder

答案1

得分: 1

这是一个类型不匹配的问题。也许问题出在占位符类型上。
你可以进行以下更改并尝试。

  1. const ListItemPlaceholder: FunctionComponent = () => {
  2. return <div/>
  3. };
  4. type ListItemComponent = ForwardRefExoticComponent<Props> & {
  5. Placeholder: typeof ListItemPlaceholder;
  6. };
  7. export const ListItem: ListItemComponent = forwardRef<
  8. HTMLDivElement,
  9. Props
  10. >((props, ref) => {
  11. //.....
  12. return <div ref={ref}/>
  13. });
  14. ListItem.Placeholder = ListItemPlaceholder;

在使用之前,你应该始终声明一个变量或类型。

英文:

This is a type-mismatch problem. And maybe the problem is with the placeholder type.
You can make these changes and try.

  1. const ListItemPlaceholder: FunctionComponent = () =&gt; {
  2. return &lt;div/&gt;
  3. };
  4. type ListItemComponent = ForwardRefExoticComponent&lt;Props&gt; &amp; {
  5. Placeholder: typeof ListItemPlaceholder;
  6. };
  7. export const ListItem: ListItemComponent = forwardRef&lt;
  8. HTMLDivElement,
  9. Props
  10. &gt;((props, ref) =&gt; {
  11. //.....
  12. return &lt;div ref={ref}/&gt;
  13. }));
  14. ListItem.Placeholder = ListItemPlaceholder;

You should always declare a variable or type before it is used.

huangapple
  • 本文由 发表于 2023年7月18日 12:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定