React.FC <Props <T >>中的GenericType

欢乐

我正在构建基于Hooks的React App,也利用了TypeScript。
我编写了一个呈现列表的组件,并且在其中props,它期望props.items类型是泛型的T

export interface ISelectableListProps<T extends { isDisableInList?: boolean }> {
    /** Specifies the item of the list. */
    items: T[];
    /** Unique REACT key used to re-initialize the component. */
    key?: React.ReactText;
    /** Specifies the template of each item. */
    itemTemplate: (item: T) => JSX.Element;
}

请注意,在界面中T还扩展了{ isDisableInList?: boolean },这是因为每个项目都可能具有该属性,用于禁用列表中的项目。

现在,关于SelectableList组件,我想写一下:

const SelectableListComponent: React.FC<ISelectableListProps<T>> = <T extends {}>(props) => { ... }

但是TypeScript给我一个错误,一个问题:

  1. 错误:类型T,在ISelectableListProps<T>没有找到;
  2. 问题:props不再推断类型:它具有any相反,通过删除<T extends {}>props推断类型为React.PropsWithChildren<ISelectableListProps<any>>;

我发现的唯一解决方法是定义一个ItemType,如下所示:

type ItemType = { isDisableInList?: boolean } & { [key: string]: any };

然后像这样编写组件:

const SelectableListComponent: React.FC<ISelectableListProps<ItemType>> = props => { ... }

但是,在使用组件时,这样做props.items应该是ItemType[],而不是通用类型。实际上,我想这样调用组件:

<SelectableList<MyType> items={...} itemTemplate={...} />

但是,正如我所说,这些项目预计将是ItemType在此处输入图片说明

顺便说一句,没有错误,因为ItemTypehas给出了,& { [key: string]: any }但是在我看来,这似乎是“ hacky”,几乎像using一样any

有什么办法可以实现我想要的?

提香·切尔尼科娃·德拉戈米尔

无法使用React.FC定义通用组件的方法。您可以只使用简单的函数定义或箭头函数,但是让TS推断类型:

export interface ISelectableListProps<T extends { isDisableInList?: boolean }> {
    /** Specifies the item of the list. */
    items: T[];
    /** Unique REACT key used to re-initialize the component. */
    key?: React.ReactText;
    /** Specifies the template of each item. */
    itemTemplate: (item: T) => JSX.Element;
}

const SelectableListComponent = <T extends {}>(props: ISelectableListProps<T>) => <></>


游乐场链接

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

与Preact中的React.FC类型等效

来自分类Dev

当子节点可以是React节点或函数时如何使用React.FC <props>类型

来自分类Dev

转发ref以进行react.FC给出类型错误:类型'IntrinsicAttributes&Props&{children:?ReactNode}

来自分类Dev

在 React 中传递 props

来自分类Dev

当带有错误道具的React功能组件(React.FC <Props>)作为道具发送时,Typescript不会抱怨吗?

来自分类Dev

在React.FC中具有默认值的表单

来自分类Dev

过滤React Props中的链接

来自分类Dev

在React中基于this.props的动态导入

来自分类Dev

如何在React中引用组件的props?

来自分类Dev

React.js在props中传递=>

来自分类Dev

在 React 中通过 props 传递状态

来自分类Dev

命名空间“ React”没有导出的成员“ FC”。在'@ types / reactour'中

来自分类Dev

对React的误解。FC和类组件的交互

来自分类Dev

像React中的{... props}一样,如何在Vue中破坏props?

来自分类Dev

在React中的props对象上访问键值对中的键名

来自分类Dev

如何将props传递给在React中作为props传递的组件?

来自分类Dev

React.js this.props未在链接元素中呈现

来自分类Dev

ES 6 React类在getter中访问props

来自分类Dev

React:如何访问this.props.children中的引用

来自分类Dev

无法从React组件中的props访问历史记录?

来自分类Dev

如何在React TypeScript中实现e和props?

来自分类Dev

在React Native中通过props从数组设置文本

来自分类Dev

React Navigation Stack.Screen中的{... props}是什么?

来自分类Dev

在React功能组件中的函数内部调用props.myFunction()

来自分类Dev

在React TypeScript的... props中为onClick属性添加类型

来自分类Dev

在React.cloneElement()中覆盖props时出错

来自分类Dev

React / Firebase-无法从auth函数中调用this.props

来自分类Dev

根据react-native中的props值动态加载图像?

来自分类Dev

this.props.dispatch 不是 react js 组件文件中的函数

Related 相关文章

  1. 1

    与Preact中的React.FC类型等效

  2. 2

    当子节点可以是React节点或函数时如何使用React.FC <props>类型

  3. 3

    转发ref以进行react.FC给出类型错误:类型'IntrinsicAttributes&Props&{children:?ReactNode}

  4. 4

    在 React 中传递 props

  5. 5

    当带有错误道具的React功能组件(React.FC <Props>)作为道具发送时,Typescript不会抱怨吗?

  6. 6

    在React.FC中具有默认值的表单

  7. 7

    过滤React Props中的链接

  8. 8

    在React中基于this.props的动态导入

  9. 9

    如何在React中引用组件的props?

  10. 10

    React.js在props中传递=>

  11. 11

    在 React 中通过 props 传递状态

  12. 12

    命名空间“ React”没有导出的成员“ FC”。在'@ types / reactour'中

  13. 13

    对React的误解。FC和类组件的交互

  14. 14

    像React中的{... props}一样,如何在Vue中破坏props?

  15. 15

    在React中的props对象上访问键值对中的键名

  16. 16

    如何将props传递给在React中作为props传递的组件?

  17. 17

    React.js this.props未在链接元素中呈现

  18. 18

    ES 6 React类在getter中访问props

  19. 19

    React:如何访问this.props.children中的引用

  20. 20

    无法从React组件中的props访问历史记录?

  21. 21

    如何在React TypeScript中实现e和props?

  22. 22

    在React Native中通过props从数组设置文本

  23. 23

    React Navigation Stack.Screen中的{... props}是什么?

  24. 24

    在React功能组件中的函数内部调用props.myFunction()

  25. 25

    在React TypeScript的... props中为onClick属性添加类型

  26. 26

    在React.cloneElement()中覆盖props时出错

  27. 27

    React / Firebase-无法从auth函数中调用this.props

  28. 28

    根据react-native中的props值动态加载图像?

  29. 29

    this.props.dispatch 不是 react js 组件文件中的函数

热门标签

归档