Search code examples
reactjstypescriptmaterial-uimaterial-table

react-material table with typescript shows generic type error


I'm trying to run react-material under a Typescript project.

Since I'm newbie with Typescript I got some errors I don't know how to solve them.

In this guest I'm trying to create a reusable React component. (Please open the guest to see the full code)

As I said the example above shows some errors, this is an example in the line 48 in homePage :

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)

Why this array shows an error ?

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

Despite the interface defines clearly that type accept string :

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

It would be helpful if someone have an idea how to get this work.


Solution

  • SOLVED ! My god it took me so much to handle this typescript errors ...

    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
      }
    ];