Replies: 1 comment
-
You can use creator function with type parameter: import { type ColumnDef } from "@tanstack/react-table";
interface MyTableData {
name: string;
resourceId: string | undefined;
}
function createResourceIdColumn<
TData extends { resourceId: string | undefined },
>(): ColumnDef<TData> {
const columnResourceId: ColumnDef<TData> = {
accessorKey: "resourceId",
cell: (info) => <div>{info.row.original.resourceId}</div>,
header: "RESOURCE ID",
};
return columnResourceId;
}
const columns: Array<ColumnDef<MyTableData>> = [
{
accessorKey: "name",
cell: (info) => <div>{info.row.original.name}</div>,
},
// No error here:
createResourceIdColumn(),
]; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My app has a bunch of tables that have similar columns, and I'm trying to ensure those columns look / behave identically across all tables. These definitions are bit involved, with custom headers and value displays, and it's very easy for me to make a change to that column definition in one table but forget to update other tables to match. So I want to extract that column definition to be reusable by multiple tables.
I'm stuck on making that definition type-safe. For instance, if I have a column called
resourceId
that's typed asstring | undefined
, then I could have something like:And then insert it into my table definition as such: (
MyTableData
includes aresourceId
key):This works just fine, but Typescript complains with an error similar to:
So is there a proper way of typing sharable column definitions, or is this something that's not supported and my code above may break in some situations?
Beta Was this translation helpful? Give feedback.
All reactions