首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有typescript的react-material表显示一般类型错误

带有typescript的react-material表显示一般类型错误
EN

Stack Overflow用户
提问于 2019-11-08 18:07:08
回答 3查看 4.1K关注 0票数 3

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

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

在这个guest中,我试图创建一个可重用的React组件。(请打开访客查看完整代码)

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

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
columns = [...{
    title: "Birth Place",
    field: "birthCity",
    type: "string" // ERROR ?!
}]

尽管接口清楚地定义了该类型接受字符串:

代码语言:javascript
复制
type?: ('string' | 'boolean' | 'numeric' | 'date' | 'datetime' | 'time' | 'currency');

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-11-08 22:31:09

解决了!天哪,我花了这么多时间来处理这个打字错误...

代码语言:javascript
复制
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
  }
];
票数 2
EN

Stack Overflow用户

发布于 2020-02-21 23:51:56

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

代码语言:javascript
复制
columns = [...{
    title: "Birth Place",
    field: "birthCity",
    type: "string" as const // ERROR ?!
}]
票数 4
EN

Stack Overflow用户

发布于 2019-11-15 19:41:38

对于列类型,string实际上不是可接受的类型:

我创建了一个PR来解决这个问题,但是如果你要显示一个字符串,你可以省略这个类型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58764420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档