Repository navigation
Combine two columnHelpers extending from each other #6665
|
Hi everyone, im have a lot of interface types extending from one base interface. i created columns based on this base interface and want to add merge them into the other columns. A simplified version of this is interface testA {
a: string
}
interface testB extends testA {
b: string
}
const columnHelperTestA = createColumnHelper<AppFeatures, testA>();
const columnHelperTestB = createColumnHelper<AppFeatures, testB>();
const columnA = columnHelperTestA.accessor('a', {
id: 'a'
})
const columnB = columnHelperTestB.accessor('b', {
id: 'b'
})
const columns = columnHelperTestB.columns([
columnA,
columnB,
])Although this shows an error that column cant be added to columns because its missing the property 'B'. Is this even possible or what is the prefered way of doing this? i want to create reusable columns for different interfaces |
Replies: 3 comments
|
Hi @lks-b, The reason TypeScript throws an error is due to type variance in Here are the two idiomatic ways to achieve reusable base columns cleanly in TanStack Table: Solution 1: Generic Column Factory (Recommended & Cleanest)Instead of creating a statically typed column helper for import { createColumnHelper } from '@tanstack/react-table';
interface testA {
a: string;
}
interface testB extends testA {
b: string;
}
// 1. Generic function for base columns
function getBaseColumns<TData extends testA>() {
const helper = createColumnHelper<any, TData>();
return [
helper.accessor('a' as any, {
id: 'a',
header: 'Column A',
}),
];
}
// 2. Combine in testB
const columnHelperB = createColumnHelper<any, testB>();
const columns = [
...getBaseColumns<testB>(),
columnHelperB.accessor('b', {
id: 'b',
header: 'Column B',
}),
];Solution 2: Explicit Cast on Reusable DefinitionsIf you prefer declaring import { ColumnDef } from '@tanstack/react-table';
const columnA = columnHelperTestA.accessor('a', {
id: 'a',
});
const columnB = columnHelperTestB.accessor('b', {
id: 'b',
});
const columns = [
columnA as unknown as ColumnDef<testB, any>,
columnB,
];Because Solution 1 (the generic factory function) is the standard pattern used across large design systems and monorepos for maximum reusability across multiple extending models. Hope this helps! If this solves your issue, please feel free to mark this as the accepted answer. |
|
Column callbacks take the row type, so a const aColumn = <T extends testA>() =>
createColumnHelper<AppFeatures, T>().accessor((row) => row.a, { id: 'a' })
const columns = columnHelperTestB.columns([aColumn<testB>(), columnB]) |
Why this error occursWhile it intuitively feels like Internally,
Recommended Solution: Generic Column Factory PatternThe most idiomatic and type-safe pattern in TanStack Table for sharing columns across inheriting interfaces is to define the reusable columns inside a generic helper function constrained by import { createColumnHelper, type ColumnDef } from '@tanstack/react-table';
interface testA {
a: string;
}
interface testB extends testA {
b: string;
}
// 1. Define reusable base columns as a generic factory:
export function getBaseColumns<T extends testA>(): ColumnDef<T, any>[] {
const helper = createColumnHelper<T>();
return [
helper.accessor('a', {
id: 'a',
header: 'Field A',
}),
];
}
// 2. Instantiate for testB:
const helperB = createColumnHelper<testB>();
export const columnsTestB = [
...getBaseColumns<testB>(),
helperB.accessor('b', {
id: 'b',
header: 'Field B',
}),
];Why this pattern works best:
Alternative: Type Casting Existing Single Column InstancesIf you already have pre-constructed const columns = columnHelperTestB.columns([
columnA as unknown as ColumnDef<testB, any>,
columnB,
]); |
Column callbacks take the row type, so a
testAcolumn cannot go intestBcolumns. Make it more generic: