TypeScript 泛型与 React 函数组件

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

TypeScript Generics with React Function Components

问题

You're encountering a TypeScript error because you're trying to pass a type argument (SpecificJobProps) to SearchFilterJobs, but it appears that the component is not set up to accept type arguments. You can modify the SearchFilterJobs component like this to accept a type argument:

export const SearchFilterJobs = <T extends JobPosting>(props: SearchFilterJobsProps<T> & React.HTMLAttributes<HTMLDivElement>) => {
  // ... component logic here
};

With this modification, you should be able to use it as you intended:

<SearchFilterJobs<SpecificJobProps> jobs={{ data: specificJobs }} />

This should resolve the TypeScript error you mentioned.

英文:

I have the following component structure:

export interface SearchFilterJobsProps&lt;J = JobPosting&gt; {
  unwrap?: boolean;
  jobs?: {
    data: J[];
    component?: React.ComponentType | React.ElementType;
  };
  searchMatchProps?: (keyof J)[];
}

export type SearchFilterJobsType&lt;T = JobPosting&gt; = React.FC&lt;SearchFilterJobsProps&lt;T&gt;&gt;;

export const SearchFilterJobs: SearchFilterJobsType = &lt;Z extends JobPosting&gt;(
  {
    jobs,
    searchMatchProps,
    className,
    children,
    ...rest
  }: SearchFilterJobsProps&lt;Z&gt; &amp; React.HTMLAttributes&lt;HTMLDivElement&gt;) =&gt; { ... };

I have set it up like this, because I would like this component to handle various data structures. But I seem to have made some mistakes, because when I am attempting to use it:

&lt;SearchFilterJobs&lt;SpecificJobProps&gt; jobs={{data: specificJobs}} /&gt;

TypeScript tells me: TS2558: Expected 0 type arguments, but got 1..

What am I doing wrong here?

答案1

得分: 0

以下是您提供的代码的翻译部分:

export const SearchFilterJobs: SearchFilterJobsType = ...
将为 SearchFilterJobsType 选择默认的类型参数右侧的内容不重要 - 如果提供了类型注释则必须完整

在这种情况下最简单的解决方案是在常量上使用 `satisfies` 而不是类型注释

export type SearchFilterJobsType<T = JobPosting> = React.FC<SearchFilterJobsProps<T>>;

export const SearchFilterJobs = <Z extends JobPosting>(
  {
    jobs,
    searchMatchProps,
    className,
    children,
    ...rest
  }: SearchFilterJobsProps<Z> & React.HTMLAttributes<HTMLDivElement>) => { 
... } satisfies React.FC<SearchFilterJobsProps<T>>;

希望这对您有所帮助。如果您需要任何进一步的翻译,请随时提问。

英文:

Something like:

export const SearchFilterJobs: SearchFilterJobsType = ...

will pick the default type param for SearchFilterJobsType. What's on the right side doesn't matter - the type annotation, if provided, must be complete.

Simplest solution in this case is to use satisfies instead of an annotation on the constant:

export type SearchFilterJobsType&lt;T = JobPosting&gt; = React.FC&lt;SearchFilterJobsProps&lt;T&gt;&gt;;

export const SearchFilterJobs = (&lt;Z extends JobPosting&gt;(
  {
    jobs,
    searchMatchProps,
    className,
    children,
    ...rest
  }: SearchFilterJobsProps&lt;Z&gt; &amp; React.HTMLAttributes&lt;HTMLDivElement&gt;) =&gt; { 
... }) satisfies React.FC&lt;SearchFilterJobsProps&lt;T&gt;&gt;;

答案2

得分: 0

尝试将组件的const重写为函数声明。
我认为那应该可以工作 TypeScript 泛型与 React 函数组件

英文:

Try rewriting the component const to a function declaration.
I think that should work TypeScript 泛型与 React 函数组件

huangapple
  • 本文由 发表于 2023年5月11日 17:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226081.html
匿名

发表评论

匿名网友

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

确定