gpt4 book ai didi

reactjs - 带有 typescript 的 react Material 表显示通用类型错误

转载 作者:行者123 更新时间:2023-12-04 16:28:07 25 4
gpt4 key购买 nike

我正在尝试运行 react-material在 Typescript 项目下。

由于我是 Typescript 的新手,我遇到了一些错误,我不知道如何解决它们。

在此guest我正在尝试创建一个可重用的 React 组件。 (请打开客人查看完整代码)

正如我所说,上面的示例显示了一些错误,这是 homePage 第 48 行中的示例:

No overload matches this call.
Overload 1 of 2, '(props: Readonly<MaterialTableProps<IData>>): KTable<IData>', gave the following error.
Type '{ title: string; field: string; type: string; }[]' is not assignable to type 'Column<IData>[]'.
Type '{ title: string; field: string; type: string; }' is not assignable to type 'Column<IData>'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"string" | "boolean" | "time" | "numeric" | "date" | "datetime" | "currency" | undefined'.
Overload 2 of 2, '(props: MaterialTableProps<IData>, context?: any): KTable<IData>', gave the following error.
Type '{ title: string; field: string; type: string; }[]' is not assignable to type 'Column<IData>[]'.ts(2769)

为什么这个数组显示错误?

columns = [...{
title: "Birth Place",
field: "birthCity",
type: "string" // ERROR ?!
}]

尽管接口(interface)明确定义了类型接受字符串:

type?: ('string' | 'boolean' | 'numeric' | 'date' | 'datetime' | 'time' | 'currency');

如果有人知道如何完成这项工作,将会很有帮助。

最佳答案

使用原始字符串值 string 将被解释为不正确类型的字符串。要解决这个问题,请将其转换为 const

columns = [...{
title: "Birth Place",
field: "birthCity",
type: "string" as const // ERROR ?!
}]

关于reactjs - 带有 typescript 的 react Material 表显示通用类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com