TypeScript 泛型与 React 函数组件

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

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:

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

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

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

This should resolve the TypeScript error you mentioned.

英文:

I have the following component structure:

  1. export interface SearchFilterJobsProps&lt;J = JobPosting&gt; {
  2. unwrap?: boolean;
  3. jobs?: {
  4. data: J[];
  5. component?: React.ComponentType | React.ElementType;
  6. };
  7. searchMatchProps?: (keyof J)[];
  8. }
  9. export type SearchFilterJobsType&lt;T = JobPosting&gt; = React.FC&lt;SearchFilterJobsProps&lt;T&gt;&gt;;
  10. export const SearchFilterJobs: SearchFilterJobsType = &lt;Z extends JobPosting&gt;(
  11. {
  12. jobs,
  13. searchMatchProps,
  14. className,
  15. children,
  16. ...rest
  17. }: 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:

  1. &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

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

  1. export const SearchFilterJobs: SearchFilterJobsType = ...
  2. 将为 SearchFilterJobsType 选择默认的类型参数右侧的内容不重要 - 如果提供了类型注释则必须完整
  3. 在这种情况下最简单的解决方案是在常量上使用 `satisfies` 而不是类型注释
  4. export type SearchFilterJobsType<T = JobPosting> = React.FC<SearchFilterJobsProps<T>>;
  5. export const SearchFilterJobs = <Z extends JobPosting>(
  6. {
  7. jobs,
  8. searchMatchProps,
  9. className,
  10. children,
  11. ...rest
  12. }: SearchFilterJobsProps<Z> & React.HTMLAttributes<HTMLDivElement>) => {
  13. ... } satisfies React.FC<SearchFilterJobsProps<T>>;

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

英文:

Something like:

  1. 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:

  1. export type SearchFilterJobsType&lt;T = JobPosting&gt; = React.FC&lt;SearchFilterJobsProps&lt;T&gt;&gt;;
  2. export const SearchFilterJobs = (&lt;Z extends JobPosting&gt;(
  3. {
  4. jobs,
  5. searchMatchProps,
  6. className,
  7. children,
  8. ...rest
  9. }: SearchFilterJobsProps&lt;Z&gt; &amp; React.HTMLAttributes&lt;HTMLDivElement&gt;) =&gt; {
  10. ... }) 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:

确定