Currently, I am tackling a grid project in React and have come across specific types and interfaces (view code here):
export type DataGridColumnType = 'currency' | 'date' | 'number' | 'text';
interface CurrencyColumnOptions {
symbol: string;
}
interface DateColumnOptions {
format: string;
}
interface NumberColumnOptions {
decimalPoint: string;
thousandSeparator: string;
}
type TextColumnOptions = {};
export interface DataGridColumn {
field: string;
label: string;
title: string;
columnOptions?: {}; // either CurrencyColumnOptions, DateColumnOptions, NumberColumnOptions or TextColumnOptions based on the "type" property
type?: DataGridColumnType;
}
export interface DataGrid {
columns: DataGridColumn[];
rows: Record<string, unknown>[];
}
const column: DataGridColumn = {
field: 'id',
label: 'ID',
title: 'ID',
type: 'date',
columnOptions: {
format: 'YYYY-mm-dd',
},
};
My objective is to switch the type of the "columnOptions" property in "DataGridColumn" based on the value of "type". For instance, when "type" is "date", I want to utilize DateColumnOptions for the "columnOptions" property.
I understand that utilizing a generic could be a solution, but these interfaces are buried deep within my grid system, and introducing a generic might complicate the grid usage.
Researching this issue has not yielded definitive results. Can this be achieved, or must I create multiple interfaces and apply union types?
Your insights would be greatly appreciated.