我正在尝试在Typescript项目下运行react-material。
由于我是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 ?!
}]
尽管接口清楚地定义了类型接受字符串:
type?: ('string' | 'boolean' | 'numeric' | 'date' | 'datetime' | 'time' | 'currency');
如果有人对如何进行这项工作有所了解,将会很有帮助。
答案 0 :(得分:2)
使用原始字符串值string
将被解释为不是正确类型的字符串。
要解决该问题,请将其强制转换为const
。
columns = [...{
title: "Birth Place",
field: "birthCity",
type: "string" as const // ERROR ?!
}]
答案 1 :(得分:1)
答案 2 :(得分:0)
已解决!我的天啊,处理这个打字稿错误花了我很多时间...
type IType =
| "string"
| "boolean"
| "numeric"
| "date"
| "datetime"
| "time"
| "currency";
const string: IType = "string";
const columns = [
{
title: "Name",
field: "name",
type: string
},
{
title: "Surname",
field: "surname",
type: string
},
{
title: "Birth Year",
field: "birthYear",
type: string
},
{
title: "Birth Place",
field: "birthCity",
// lookup: { 34: "İstanbul", 63: "Şanlıurfa", 1: "Berlin", 2: "Tunis" },
type: string
}
];