reactjs - 在样式组件中引用通用打字稿功能组件
问题描述
我创建了一个通用组件来呈现这样的 DataGrid:
interface Props<T extends unknown> {
header: HeaderItem[]
data?: T[],
className?: string,
children?: (row: T) => React.ReactElement[]
}
const DataGrid = <T extends unknown>({ header, data, className, children }: Props<T>): React.ReactElement | null => {
...
}
由于泛型类型,无法将其设置React.FC<Props<T>>
为类型DataGrid
(或者至少我还没有找到解决方案。现在我正在尝试使用 styled-components 在另一个组件中设置一些额外的样式。
const Container = styled.div`
${DataGrid} {
}
`
通过使用它,构建失败并显示错误消息“没有重载匹配此调用”
TypeScript error in ...:
No overload matches this call.
Overload 1 of 3, '(first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 253 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps, any>>, ...rest: Interpolation<...>[]): StyledComponent<...>', gave the following error.
Argument of type '<T extends unknown>({ header, data, className, children }: Props<T>) => React.ReactElement | null' is not assignable to parameter of type 'Interpolation<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps, any>>'.
Type '<T extends unknown>({ header, data, className, children }: Props<T>) => React.ReactElement | null' is not assignable to type 'InterpolationFunction<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps, any>>'.
Types of parameters '__0' and 'props' are incompatible.
Property 'header' is missing in type 'Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & ThemeProps<...>' but required in type 'Props<unknown>'.
Overload 2 of 3, '(first: TemplateStringsArray | CSSObject | InterpolationFunction<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | ... 253 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & Props<...>, any>>, ...rest: Interpolation<...>[]): StyledComponent<...>', gave the following error.
Argument of type '<T extends unknown>({ header, data, className, children }: Props<T>) => React.ReactElement | null' is not assignable to parameter of type 'Interpolation<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & Props<...>, any>>'.
Type '<T extends unknown>({ header, data, className, children }: Props<T>) => React.ReactElement | null' is not assignable to type 'InterpolationFunction<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & Props<...>, any>>'.
Type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null' is not assignable to type 'Interpolation<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & Props<...>, any>>'.
Type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>' is not assignable to type 'Interpolation<ThemedStyledProps<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "slot" | "style" | "title" | ... 251 more ... | "onTransitionEndCapture"> & { ...; } & ColumnProps & Props<...>, any>>'.
Type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>' is not assignable to type 'CSSObject'.
Index signature is missing in type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>'. TS2769
当我删除泛型时,一切正常。有没有办法将引用的样式与泛型一起使用?
解决方案
推荐阅读
- python - 令牌匹配查询不存在
- c# - 当查询包含“\”时,如何使用 C# 中的 WIQL 执行 VSTS 查询
- c# - 将数据库从 MySQL 迁移到 SQL Server,Entity Framework 仍然尝试使用 MySQL
- numpy - 需要帮助了解 GPU 上的内核传输速度(numba、cupy、cuda)
- android - 如果我把它放在 AppBarLayout 中,为什么 TextInputLayout 颜色不会改变?
- javascript - 脚本部分php(laravel)中的json_encode看不到变量
- batch-file - 从 C:\Windows 文件夹中删除文件
- hybris - Hybris 中媒体类型的 Impex
- r - 连接变量中的多个字符元素并重新编码为 0 和 1
- sql - 乐观锁定义:用“丢失更新”和“过时读取”检测表示