简体   繁体   English

如何在 TypeScript 中正确键入通用 React function 组件

[英]How to correctly type a generic React function component in TypeScript

In the following TypeScript Playground example I tried to generalise the function component Element into a GenericElement component but TypeScript complains about the syntax.在下面的TypeScript Playground示例中,我尝试将 function 组件Element概括为GenericElement组件,但 TypeScript 抱怨语法。

How to correctly type a generic react function component in TypeScript using the React.FC type definition approach?如何使用React.FC类型定义方法在 TypeScript 中正确键入通用反应 function 组件?

import React from 'react';

type PropsType = {
  id: string,
  value: string,
};

type GenericPropsType<keyType> = {
  id: keyType,
  value: string,
};

const Element: React.FC<PropsType> = ({ id, value }) => {
  return <div>{id.toString()}={value}</div>;
};

const GenericElement: React.FC<GenericPropsType<keyType>> = <keyType = string>({ id, value }) => {
  return <div>{id.toString()}={value}</div>;
};
Type 'Element' is not assignable to type 'FC<GenericPropsType<any>>'.
Type 'Element' provides no match for the signature '(props: PropsWithChildren<GenericPropsType<any>>, context?: any): ReactElement<any, any> | null'.
Cannot find name 'keyType'.
Property 'keyType' does not exist on type 'JSX.IntrinsicElements'.
Cannot find name 'id'.
Left side of comma operator is unused and has no side effects.
Cannot find name 'value'.
Cannot find name 'id'.
Cannot find name 'value'.
Identifier expected.
Unexpected token. Did you mean `{'>'}` or `&gt;`?
Expression expected.
Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
JSX element 'keyType' has no corresponding closing tag.
'</' expected.
'Element' is declared but its value is never read.

I think it works using Higher-order function:我认为它可以使用高阶 function:

import React, { FC } from 'react';

type GenericPropsType<T = any> = {
  id: T,
  value: string,
};

const HoC = <T,>(): FC<GenericPropsType<T>> => (props) => <div></div>

const WithString = HoC<string>() // React.FC<GenericPropsType<string>>

Drawback: you have function overhead only because of type缺点:你有 function 开销只是因为类型

I don't believe my answer is helpful, because you should explicitly define generic type, or you need to pass an argument in order to infer it:我不相信我的回答有帮助,因为您应该明确定义泛型类型,或者您需要传递一个参数才能推断它:

const HoC = <T,>(a:T): FC<GenericPropsType<T>> => (props) => <div></div>

const WithString = HoC('a') // React.FC<GenericPropsType<string>>

Otherwise, I suggest you to use @Aleksey L. 's solution.否则,我建议您使用@Aleksey L.的解决方案。

Based on the explanation of @Aleksey L., I came to the following complete example that might be helpful to others:根据@Aleksey L. 的解释,我得出了以下可能对其他人有帮助的完整示例:

import React from 'react';
import ReactDOM from 'react-dom';

type GenericPropsType<keyType> = {
  id: keyType,
  value: string,
};

const GenericElement = <keyType extends string | number = string>({
  id,
  value = ''
}: GenericPropsType<keyType>): JSX.Element => {
  return (<div>{id}={value}</div>);
};

const UseGenericElement: React.FC = () => {
  return (<div><GenericElement id={4711} value="4711" /></div>);
};

ReactDOM.render(
  <div><UseGenericElement /></div>,
  document.getElementById('root');
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript/React - 如何让 function 接受通用组件类型? - TypeScript/React - How to have function accept a generic component type? 如何正确使用 Typescript 中的 React.lazy() 来导入带有泛型类型参数的反应组件? - How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter? Typescript React 组件的通用类型 - Typescript generic type for a React component 如何将带有通用道具的React类组件转换为Typescript中的功能组件? - How to transform a React Class Component with generic props to Function Component in Typescript? Typescript React 泛型函数类型 - Typescript React generic function type 如何正确键入作为函数参数的React组件? - How to correctly Type a React Component that is a param of a Function? 通用无状态组件 React 的类型? 或在 typescript 中扩展通用 function 接口以具有进一步的通用性? - Type of Generic Stateless Component React? OR Extending generic function interface in typescript to have a further generic? 打字稿:通用反应组件上儿童的类型推断 - Typescript: type inference for children on generic react component 具有函数重载和通用参数的 Typescript React 组件 - Typescript React component with function overload and generic params 如何在箭头 function React-TypeScript 中返回通用类型 - How to return Generic Type in arrow function React-TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM