Yes, it's definitely possible — and often desirable — to type rows such that the keys must correspond to the column keys defined in the columns array. However, in plain TypeScript, ensuring strict type enforcement between a dynamic array of columns and the keys of an object isn't directly supported without some form of generic or pre-defined, static typing.
Let’s break it down and walk through possible approaches.
✅ Goal
You want:
rows to have exactly the keys that exist in columns.map(c => c.key)
- Preferably at compile time via TypeScript
⚠️ Problem with Current Design
In your current definition:
export interface DataTableItem {
key: number;
columns: DataTableColumn[];
rows: {
[key: string]: string | number;
}
}
TypeScript has no way of knowing the actual allowed keys in rows, because columns is just a runtime array.
✅ Approach 1: Use Generics to Infer Column Keys
You can refactor the code to use generics, turning the column definitions into a strongly typed structure, and using the same type to enforce row key correctness.
interface DataTableColumn<K extends string = string> {
key: K;
label: string;
numeric?: boolean;
width?: number;
}
type DataTableRow<Keys extends string> = {
[K in Keys]: string | number;
};
interface DataTableItem<Columns extends readonly DataTableColumn[]> {
key: number;
columns: Columns;
rows: DataTableRow<Columns[number]['key']>;
}
Now usage would look like:
const columns = [
{ key: 'name', label: 'Name' },
{ key: 'age', label: 'Age', numeric: true },
] as const;
const item: DataTableItem<typeof columns> = {
key: 1,
columns,
rows: {
name: 'Alice',
age: 30,
// city: 'NYC', // ❌ Error: city is not defined in columns
}
};
This gives you full type safety between columns and rows.
✨ Bonus: Helper Function to Make This Nicer
You can make your life easier by creating a constructor/helper to tightly couple columns and rows:
function createDataTableItem<const C extends readonly DataTableColumn[]>(
input: {
key: number;
columns: C;
rows: DataTableRow<C[number]['key']>;
}
): DataTableItem<C> {
return input;
}
const item = createDataTableItem({
key: 1,
columns: [
{ key: 'username', label: 'User' },
{ key: 'score', label: 'Score', numeric: true },
] as const,
rows: {
username: 'John',
score: 99,
// extra: "oops" // ❌ Compiler error
},
});
🔁 Alternative: Use Fixed Schema Instead of Flexible One
If you're always working with known columns per table, and don’t need dynamic column definitions, it could be easier to define a fixed structure:
interface UserRow {
name: string;
age: number;
}
const columns: DataTableColumn<keyof UserRow>[] = [
{ key: 'name', label: 'Name' },
{ key: 'age', label: 'Age', numeric: true },
];
const rows: UserRow = {
name: 'Bob',
age: 40,
};
But this only works when the column schema is fixed.
🧹 Should You Use string[] Instead of Array of Column Objects?
Short answer: No, don’t reduce to string[] unless you only care about keys. You’d lose the ability to attach metadata like:
So use column objects + generics as shown above.
✅ Final Recommendation
Use this structure:
interface DataTableColumn<K extends string = string> {
key: K;
label: string;
numeric?: boolean;
width?: number;
}
type DataTableRow<Keys extends string> = {
[K in Keys]: string | number;
};
interface DataTableItem<Columns extends readonly DataTableColumn[]> {
key: number;
columns: Columns;
rows: DataTableRow<Columns[number]['key']>;
}
And create data like this:
const columns = [
{ key: 'product', label: 'Product' },
{ key: 'price', label: 'Price', numeric: true },
] as const;
const item: DataTableItem<typeof columns> = {
key: 123,
columns,
rows: {
product: "Laptop",
price: 1299,
},
};
This gives you static type-checking, avoids any runtime mapping risks, and keeps column metadata intact.
Let me know if you want a type-safe DataTable component based on this!