From 2486fe2b1e70d972dd959dd76cea9567edaf2320 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg <39033624+LeeLenaleee@users.noreply.github.com> Date: Tue, 31 May 2022 20:01:39 +0200 Subject: [PATCH] Allow for each dataset to be individually typed without a main type (#10387) * allow for each dataset to be individually typed without a main type * fix lint issues * add extra test cases --- types/index.esm.d.ts | 39 ++++++++++++++++++--- types/tests/chart_types.ts | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 types/tests/chart_types.ts diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index adbdca76587..913baee9274 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -485,7 +485,7 @@ export declare class Chart< readonly id: string; readonly canvas: HTMLCanvasElement; readonly ctx: CanvasRenderingContext2D; - readonly config: ChartConfiguration; + readonly config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset; readonly width: number; readonly height: number; readonly aspectRatio: number; @@ -501,7 +501,7 @@ export declare class Chart< data: ChartData; options: ChartOptions; - constructor(item: ChartItem, config: ChartConfiguration); + constructor(item: ChartItem, config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset); clear(): this; stop(): this; @@ -2087,9 +2087,9 @@ export class BasePlatform { isAttached(canvas: HTMLCanvasElement): boolean; /** * Updates config with platform specific requirements - * @param {ChartConfiguration} config + * @param {ChartConfiguration | ChartConfigurationCustomTypes} config */ - updateConfig(config: ChartConfiguration): void; + updateConfig(config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset): void; } export class BasicPlatform extends BasePlatform {} @@ -3628,6 +3628,11 @@ export interface ChartDatasetProperties { data: TData; } +export interface ChartDatasetPropertiesCustomTypesPerDataset { + type: TType; + data: TData; +} + export type ChartDataset< TType extends ChartType = ChartType, TData = DefaultDataPoint @@ -3635,6 +3640,13 @@ export type ChartDataset< { [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TType] > & ChartDatasetProperties; +export type ChartDatasetCustomTypesPerDataset< + TType extends ChartType = ChartType, + TData = DefaultDataPoint +> = DeepPartial< +{ [key in ChartType]: { type: key } & ChartTypeRegistry[key]['datasetOptions'] }[TType] +> & ChartDatasetPropertiesCustomTypesPerDataset; + /** * TData represents the data point type. If unspecified, a default is provided * based on the chart type. @@ -3649,6 +3661,15 @@ export interface ChartData< datasets: ChartDataset[]; } +export interface ChartDataCustomTypesPerDataset< + TType extends ChartType = ChartType, + TData = DefaultDataPoint, + TLabel = unknown +> { + labels?: TLabel[]; + datasets: ChartDatasetCustomTypesPerDataset[]; +} + export interface ChartConfiguration< TType extends ChartType = ChartType, TData = DefaultDataPoint, @@ -3659,3 +3680,13 @@ export interface ChartConfiguration< options?: ChartOptions; plugins?: Plugin[]; } + +export interface ChartConfigurationCustomTypesPerDataset< + TType extends ChartType = ChartType, + TData = DefaultDataPoint, + TLabel = unknown +> { + data: ChartDataCustomTypesPerDataset; + options?: ChartOptions; + plugins?: Plugin[]; +} diff --git a/types/tests/chart_types.ts b/types/tests/chart_types.ts new file mode 100644 index 00000000000..f05bda4da86 --- /dev/null +++ b/types/tests/chart_types.ts @@ -0,0 +1,69 @@ +import { Chart } from '../index.esm'; + +const chart = new Chart('chart', { + type: 'bar', + data: { + labels: ['1', '2', '3'], + datasets: [{ + data: [1, 2, 3] + }, + { + data: [1, 2, 3], + categoryPercentage: 10 + }], + } +}); + +const chart2 = new Chart('chart', { + type: 'bar', + data: { + labels: ['1', '2', '3'], + datasets: [{ + type: 'line', + data: [1, 2, 3], + // @ts-expect-error should not allow bar properties to be defined in a line dataset + categoryPercentage: 10 + }, + { + type: 'line', + pointBackgroundColor: 'red', + data: [1, 2, 3] + }, + { + data: [1, 2, 3], + categoryPercentage: 10 + }], + } +}); + +const chart3 = new Chart('chart', { + data: { + labels: ['1', '2', '3'], + datasets: [{ + type: 'bar', + data: [1, 2, 3], + categoryPercentage: 10 + }, + { + type: 'bar', + data: [1, 2, 3], + // @ts-expect-error should not allow line properties to be defined in a bar dataset + pointBackgroundColor: 'red', + }], + } +}); + +// @ts-expect-error all datasets should have a type property or a default fallback type should be set +const chart4 = new Chart('chart', { + data: { + labels: ['1', '2', '3'], + datasets: [{ + type: 'bar', + data: [1, 2, 3], + categoryPercentage: 10 + }, + { + data: [1, 2, 3] + }], + } +});