I am trying to create a component props interface that has several top level properties for global configuration and a configuration builder for an arbitrary number of ValueAxis configration objects.
TDateKey is used to ensure that all data series object arrays at minimum have a TDateKey ie. "time". this works great for a singular ValueAxis configuration, however each subsequent confguration added will only allow its data type to be of the type created in the first one. I think this is because the first ValueAxes object defined hijacks the TData type. is there any way to have a TData type per ValueAxes object such that each one has its own typesafety? (please see codesandbox)
export interface ChartProps<
TDateKey extends string,
TData extends ChartTimeSeries<TDateKey>
> {
aggregation: string;
dateKey: TDateKey;
id: string;
valueAxes: ValueAxesConfig<TDateKey, TData>;
}
export type DateKeyDatum<T extends string> = {
[key in T]: number;
};
export type ChartTimeSeries<T extends string> = DateKeyDatum<T> &
Record<string, number>;
export interface ValueAxesConfig<
TDateKey extends string,
TData extends ChartTimeSeries<TDateKey>
> {
[key: string]: {
data: TData[];
defaultActiveKeys: Array<keyof Omit<TData, TDateKey>>;
seriesConfig: {
[key in keyof Omit<TData, TDateKey>]: SeriesConfig;
};
};
}
export interface SeriesConfig {
alias?: string;
color: string;
}
valueAxes={{
defaultAxis: {
data: chartData.map((datum) => ({
metricOne: datum.metricOne,
time: datum.time
})),
},
/** How can i fix this and still have type safetly? **/
secondaryAxis: {
data: chartData.map((datum) => ({
metricTwo: datum.metricTwo,
time: datum.time
})),
}
}}
/>
https://codesandbox.io/s/exciting-pascal-2q2z45?file=/src/App.tsx