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

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

Problem with typescript error when create react generic component

问题

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

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

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

这是我的BaseBlock组件:

import React from 'react';

interface Props<T> {
  id?: string;
  items?: T[];
  children?: (item: T, index: number) => React.ReactNode;
  parentId?: string;
  contentId?: string[];
}

const BaseBlock = React.forwardRef(function BaseBlock<T>(
  { items, children }: React.PropsWithChildren<Props<T>>,
  ref: React.ForwardedRef<HTMLDivElement>
): React.ReactElement | null {
  return (
    <div data-testid="base-block" ref={ref}>
      {items && children && items.map(children)}
    </div>
  );
});

export type BaseBlockProps<T> = React.PropsWithRef<Props<T>>;

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.

const items = [&#39;test1&#39;, &#39;test2&#39;, &#39;test3&#39;];
const renderComp = (item: any) =&gt; {
  return &lt;div&gt;{item}&lt;/div&gt;
}
&lt;BaseBlock&lt;string&gt; items={items}&gt;{renderRow}&lt;/BaseBlock&gt;
           ^
           error

This is my BaseBlock component

import React from &#39;react&#39;;

interface Props&lt;T&gt; {
  id?: string;
  items?: T[];
  children?: (item: T, index: number) =&gt; React.ReactNode;
  parentId?: string;
  contentId?: string[];
}

const BaseBlock = React.forwardRef(function BaseBlock&lt;T&gt;(
  { items, children }: React.PropsWithChildren&lt;Props&lt;T&gt;&gt;,
  ref: React.ForwardedRef&lt;HTMLDivElement&gt;
): React.ReactElement | null {
  return (
    &lt;div data-testid=&quot;base-block&quot; ref={ref}&gt;
      {items &amp;&amp; children &amp;&amp; items.map(children)}
    &lt;/div&gt;
  );
});

export type BaseBlockProps&lt;T&gt; = React.PropsWithRef&lt;Props&lt;T&gt;&gt;;

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 的评论解决了这个问题,并参考了这个 问题

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

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

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

Finally, I used global type augmentation and solved it.

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

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:

确定