React Table Typescript „Typ nie je priraditeľný“


Nastavil som svoj React projekt s Typescript a React-Table. Postupoval som podľa Rýchleho sprievodcu na webovej stránke React-Table a dostal som chybu.

 const data = React.useMemo(
   () => [
     {
       col1: 'Hello',
       col2: 'World',
     },
     {
       col1: 'react-table',
       col2: 'rocks',
     },
     {
       col1: 'whatever',
       col2: 'you want',
     },
   ],
   []
 )

const columns = React.useMemo(
    () => [
      {
        Header: 'Column 1',
        accessor: 'col1', // accessor is the "key" in the data
      },
      {
        Header: 'Column 2',
        accessor: 'col2',
      },
    ],
    []
  )



Dostanem chybu pri vykonaní const tableInstance=useTable({ columns, data }).

Typ '{\n Header: string; accessor: string; }[]' sa nedá priradiť k typu 'readonly Column<{\n col1: string; col2: string; }>[]'. Typ '{ Header: string; accessor: string; }' sa nedá priradiť k typu 'Column<{ col1: string; col2: string; }>'.

Práve som sa začal tomu venovať, takže naozaj neviem, čo sa deje. Tu je kódový sandbox s recreáciou problému: Sandbox

Odpoveď č. 1

Zdá sa, že TypeScript zlyháva pri odvodzovaní typov, takže som mu dal nápovedu tým, že

  • Predstavujeme nový typ type Cols={ col1: string; col2: string };
  • Explicitné uvedenie typu pre const columns: Column<Cols>[]=...

Plný funkčný príklad je tu:

import { useTable, TableOptions, Column } from "react-table";
import React from "react";

type Cols = { col1: string; col2: string };

export default function App() {
  const data = React.useMemo(
    (): Cols[] => [
      {
        col1: "Hello",
        col2: "World"
      },
      {
        col1: "react-table",
        col2: "rocks"
      },
      {
        col1: "whatever",
        col2: "you want"
      }
    ],
    []
  );

  const columns: Column<{ col1: string; col2: string }>[] = React.useMemo(
    () => [
      {
        Header: "Column 1",
        accessor: "col1" // accessor is the "key" in the data
      },
      {
        Header: "Column 2",
        accessor: "col2"
      }
    ],
    []
  );

  const options: TableOptions<{ col1: string; col2: string }> = {
    data,
    columns
  };

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow
  } = useTable(options);

  return (
    <table {...getTableProps()} style={{ border: "solid 1px blue" }}>
      <thead>
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map((column) => (
              <th
                {...column.getHeaderProps()}
                style={{
                  borderBottom: "solid 3px red",
                  background: "aliceblue",
                  color: "black",
                  fontWeight: "bold"
                }}
              >
                {column.render("Header")}
              </th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map((row) => {
          prepareRow(row);
          return (
            <tr {...row.getRowProps()}>
              {row.cells.map((cell) => {
                return (
                  <td
                    {...cell.getCellProps()}
                    style={{
                      padding: "10px",
                      border: "solid 1px gray",
                      background: "papayawhip"
                    }}
                  >
                    {cell.render("Cell")}
                  </td>
                );
              })}
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

Odpoveď č. 2

Čelil som rovnakému problému, potom som našiel toto riešenie, pridajte do vášho objektu stĺpcov ''const'' takto:

[
  {
    Header: 'Id',
    accessor: 'id' as const,
  },
  {
    Header: 'Column 1',
    accessor: 'column_1' as const,
  },
  {
    Header: 'Column 2',
    accessor: 'column_2' as const,
  },
  {
    Header: 'Column 3',
    accessor: 'column_3' as const,
  },
]

Súvisiace články