问题出在创建React通用组件时的TypeScript错误。

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

Problem with typescript error when create react generic component

问题

最近,在使用React时,我遇到了一些问题。当我调用我的React通用组件BaseBlock时,我已经提供了一个类型,但是TypeScript报错说:

期望0个类型参数,但实际提供了1个.ts(2558)

我想要问如何解决这种情况。

这是我的BaseBlock组件:

  1. import React from 'react';
  2. interface Props<T> {
  3. id?: string;
  4. items?: T[];
  5. children?: (item: T, index: number) => React.ReactNode;
  6. parentId?: string;
  7. contentId?: string[];
  8. }
  9. const BaseBlock = React.forwardRef(function BaseBlock<T>(
  10. { items, children }: React.PropsWithChildren<Props<T>>,
  11. ref: React.ForwardedRef<HTMLDivElement>
  12. ): React.ReactElement | null {
  13. return (
  14. <div data-testid="base-block" ref={ref}>
  15. {items && children && items.map(children)}
  16. </div>
  17. );
  18. });
  19. export type BaseBlockProps<T> = React.PropsWithRef<Props<T>>;
  20. export default BaseBlock;

我为我的组件提供了字符串类型。期望没有错误并渲染这个组件。它的确渲染了,但TypeScript给我报错:

期望0个类型参数,但实际提供了1.ts(2558)

我认为问题出在我为我的组件提供了<string>类型,但这个组件不应该使用类型,它期望不带类型参数的组件。那么,我该如何解决这个问题?

英文:

Recently, I encountered some issues while using React. when I called my react generic component, BaseBlock.

I had already provided a type, but TypeScript threw an error saying

>Expected 0 type arguments, but got 1.ts(2558)

I would like to ask how to solve this situation.

  1. const items = [&#39;test1&#39;, &#39;test2&#39;, &#39;test3&#39;];
  2. const renderComp = (item: any) =&gt; {
  3. return &lt;div&gt;{item}&lt;/div&gt;
  4. }
  5. &lt;BaseBlock&lt;string&gt; items={items}&gt;{renderRow}&lt;/BaseBlock&gt;
  6. ^
  7. error

This is my BaseBlock component

  1. import React from &#39;react&#39;;
  2. interface Props&lt;T&gt; {
  3. id?: string;
  4. items?: T[];
  5. children?: (item: T, index: number) =&gt; React.ReactNode;
  6. parentId?: string;
  7. contentId?: string[];
  8. }
  9. const BaseBlock = React.forwardRef(function BaseBlock&lt;T&gt;(
  10. { items, children }: React.PropsWithChildren&lt;Props&lt;T&gt;&gt;,
  11. ref: React.ForwardedRef&lt;HTMLDivElement&gt;
  12. ): React.ReactElement | null {
  13. return (
  14. &lt;div data-testid=&quot;base-block&quot; ref={ref}&gt;
  15. {items &amp;&amp; children &amp;&amp; items.map(children)}
  16. &lt;/div&gt;
  17. );
  18. });
  19. export type BaseBlockProps&lt;T&gt; = React.PropsWithRef&lt;Props&lt;T&gt;&gt;;
  20. export default BaseBlock;

I give my component string type. Excepted no error and render this component. It just render, but typescript give me error
>Expected 0 type arguments, but got 1.ts(2558)

I think it is that I give the &lt;string&gt; type for my comp, but this comp should not use type, it excepted 0 argument on this comp, so how can I solved?

答案1

得分: 1

我通过 @Linda Paiste 的评论解决了这个问题,并参考了这个 问题

最后,我使用了全局类型扩充来解决它。

  1. declare module 'react' {
  2. function forwardRef<T, P = {}>(
  3. render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
  4. ): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
  5. }
英文:

I solved this problem by @Linda Paiste comment
and reference this question.

Finally, I used global type augmentation and solved it.

  1. declare module &#39;react&#39; {
  2. function forwardRef&lt;T, P = {}&gt;(
  3. render: (props: P, ref: React.Ref&lt;T&gt;) =&gt; React.ReactElement | null
  4. ): (props: P &amp; React.RefAttributes&lt;T&gt;) =&gt; React.ReactElement | null;
  5. }

huangapple
  • 本文由 发表于 2023年2月19日 12:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497978.html
匿名

发表评论

匿名网友

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

确定