Reactjs React.FC<;中的GenericType;道具<;T>&燃气轮机;
我正在构建一个基于钩子的React应用程序,还利用了TypeScript。Reactjs React.FC<;中的GenericType;道具<;T>&燃气轮机;,reactjs,typescript,react-hooks,Reactjs,Typescript,React Hooks,我正在构建一个基于钩子的React应用程序,还利用了TypeScript。 我编写了一个呈现列表的组件,在它的道具中,它需要道具.items,它的类型是泛型T: export interface ISelectableListProps<T extends { isDisableInList?: boolean }> { /** Specifies the item of the list. */ items: T[]; /** Unique REACT ke
我编写了一个呈现列表的组件,在它的
道具中,它需要道具.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;
}
但是TypeScript给了我一个错误和一个问题:
错误:TypeT
,在ISelectableListProps
中未找到李>
问题:不再推断道具的类型
:它有任何
。相反,通过删除
,props
类型被推断为React.PropsWithChildren,无法使用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>) => <></>
导出接口ISelectableListProps{
/**指定列表中的项目*/
项目:T[];;
/**用于重新初始化组件的唯一REACT键*/
键?:React.ReactText;
/**指定每个项目的模板*/
itemTemplate:(item:T)=>JSX.Element;
}
常量SelectableListComponent=(props:ISelectableListProps)=>
type ItemType = { isDisableInList?: boolean } & { [key: string]: any };
const SelectableListComponent: React.FC<ISelectableListProps<ItemType>> = props => { ... }
<SelectableList<MyType> items={...} itemTemplate={...} />
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>) => <></>