反应 Table 打字稿 "Type is not assignable"
React Table Typescript "Type is not assignable"
我已经使用 Typescript 和 React-Table 设置了我的 React 项目。
我正在关注 React-Table 网站上的 Quick Start Guide 并遇到错误。
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',
},
],
[]
)
我在执行 const tableInstance = useTable({ columns, data })
时遇到错误。
Type '{ Header: string; accessor: string; }[]' is not assignable to type 'readonly Column<{ col1: string; col2: string; }>[]'.
Type '{ Header: string; accessor: string; }' is not assignable to type 'Column<{ col1: string; col2: string; }>'.
我才刚刚开始接触这个,所以我真的不知道发生了什么。
这是一个代码沙箱,其中包含问题的再现:Sandbox
似乎 TypeScript 的类型推断失败了,所以我通过
给了他提示
- 引入新类型
type Cols = { col1: string; col2: string };
- 明确说明
const columns: Column<Cols>[] = ...
的类型
完整的工作示例在这里:
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>
);
}
如果您执行以下操作,它将为您解决此问题:
https://github.com/tannerlinsley/react-table/issues/2970#issuecomment-756364081
那么您只需要在配置文件中将列类型设置为列[]
export const headers: Column[] = [
{
Header: "ID",
accessor: "id", // accessor is the "key" in the data
},
.....
然后在你 table 消耗你做的组件
const columns = useMemo(() => headers, []);
const data = useMemo(() => people, []);
const tableInstance = useTable({ columns, data });
当然,您也可以在一个文件中完成所有操作
我已经使用 Typescript 和 React-Table 设置了我的 React 项目。 我正在关注 React-Table 网站上的 Quick Start Guide 并遇到错误。
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',
},
],
[]
)
我在执行 const tableInstance = useTable({ columns, data })
时遇到错误。
Type '{ Header: string; accessor: string; }[]' is not assignable to type 'readonly Column<{ col1: string; col2: string; }>[]'. Type '{ Header: string; accessor: string; }' is not assignable to type 'Column<{ col1: string; col2: string; }>'.
我才刚刚开始接触这个,所以我真的不知道发生了什么。 这是一个代码沙箱,其中包含问题的再现:Sandbox
似乎 TypeScript 的类型推断失败了,所以我通过
给了他提示- 引入新类型
type Cols = { col1: string; col2: string };
- 明确说明
const columns: Column<Cols>[] = ...
的类型
完整的工作示例在这里:
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>
);
}
如果您执行以下操作,它将为您解决此问题:
https://github.com/tannerlinsley/react-table/issues/2970#issuecomment-756364081
那么您只需要在配置文件中将列类型设置为列[]
export const headers: Column[] = [
{
Header: "ID",
accessor: "id", // accessor is the "key" in the data
},
.....
然后在你 table 消耗你做的组件
const columns = useMemo(() => headers, []);
const data = useMemo(() => people, []);
const tableInstance = useTable({ columns, data });
当然,您也可以在一个文件中完成所有操作