reactjs - 带有打字稿的反应材料表显示通用类型错误
问题描述
我正在尝试在 Typescript 项目下运行react-material 。
由于我是 Typescript 的新手,我遇到了一些错误,我不知道如何解决它们。
在这个客人中,我正在尝试创建一个可重用的 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 ?!
}]
尽管接口明确定义了类型接受字符串:
type?: ('string' | 'boolean' | 'numeric' | 'date' | 'datetime' | 'time' | 'currency');
如果有人知道如何完成这项工作,那将会很有帮助。
解决方案
使用原始字符串值string
将被解释为不是正确类型的字符串。为了解决这个问题,将其转换为const
.
columns = [...{
title: "Birth Place",
field: "birthCity",
type: "string" as const // ERROR ?!
}]
推荐阅读
- csv - 如何从 CSV 文件中汇总一定数量的值?
- postgresql - 如何从 postgreSQL 中的截断表中恢复数据
- java - java MVC输入类型日期不可访问
- hadoop - Hue 内部架构和 Hive 查询执行
- java - 在 Vaadin 8 中向可编辑网格添加一个空行/bean
- java - 为什么在 PostgreSQL 查询后没有执行部分 Java 代码?
- reactjs - React + Typescript + 动态导入 HOC 类型
- javascript - 在我的组件生命周期中,何时可以开始访问 this.$refs?
- nginx - Nginx proxy_pass 到 https
- java - 当前运行计划尚未完成时启动计划 Cron4j