From e836cfca6b69f34d93616b119bdbd7fa3a70aeca Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Mon, 22 Feb 2021 17:57:27 -0500 Subject: [PATCH 1/5] Expose TParsedData at top level of types --- types/index.esm.d.ts | 7 ++++--- .../test_specify_types_chart_constructor.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 types/tests/test_specify_types_chart_constructor.ts diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index aed7eea0611..65b079bddd8 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -443,13 +443,14 @@ export interface ActiveElement extends ActiveDataPoint { export declare class Chart< TType extends ChartType = ChartType, TData extends unknown[] = DefaultDataPoint, - TLabel = unknown + TLabel = unknown, + TParsedData extends unknown = ParsedDataType > { readonly platform: BasePlatform; readonly id: string; readonly canvas: HTMLCanvasElement; readonly ctx: CanvasRenderingContext2D; - readonly config: ChartConfiguration + readonly config: ChartConfiguration readonly width: number; readonly height: number; readonly aspectRatio: number; @@ -462,7 +463,7 @@ export declare class Chart< data: ChartData; options: ChartOptions; - constructor(item: ChartItem, config: ChartConfiguration); + constructor(item: ChartItem, config: ChartConfiguration); clear(): this; stop(): this; diff --git a/types/tests/test_specify_types_chart_constructor.ts b/types/tests/test_specify_types_chart_constructor.ts new file mode 100644 index 00000000000..d0a036ee905 --- /dev/null +++ b/types/tests/test_specify_types_chart_constructor.ts @@ -0,0 +1,18 @@ +import { Chart } from '../index.esm'; + +const chart = new Chart<'scatter', number[], string>('id', { + type: 'scatter', + data: { + labels: [], + datasets: [] + }, + options: {}, +}); + +interface Context { + chart: Chart<'scatter'|'bar', unknown[], unknown, unknown>; +} + +const ctx: Context = { + chart: chart +}; \ No newline at end of file From c824c754b01d184b22ed844d3b0734686ada9f9c Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Mon, 22 Feb 2021 18:03:18 -0500 Subject: [PATCH 2/5] Export parsed data types --- types/index.esm.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index 65b079bddd8..31bc855c29e 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -3131,7 +3131,7 @@ export interface ScaleTypeRegistry extends CartesianScaleTypeRegistry, RadialSca export type ScaleType = keyof ScaleTypeRegistry; -interface CartesianParsedData { +export interface CartesianParsedData { x: number; y: number; @@ -3145,7 +3145,7 @@ interface CartesianParsedData { } } -interface BarParsedData extends CartesianParsedData { +export interface BarParsedData extends CartesianParsedData { // Only specified if floating bars are show _custom?: { barStart: number; @@ -3157,12 +3157,12 @@ interface BarParsedData extends CartesianParsedData { } } -interface BubbleParsedData extends CartesianParsedData { +export interface BubbleParsedData extends CartesianParsedData { // The bubble radius value _custom: number; } -interface RadialParsedData { +export interface RadialParsedData { r: number; } From 7e6dbebb7cc1a823d65a323ef679f736c1c27d5c Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Mon, 22 Feb 2021 18:49:22 -0500 Subject: [PATCH 3/5] TParsedData extends unknown --- types/index.esm.d.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index 31bc855c29e..92cbbc88737 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -38,7 +38,7 @@ export { ScriptableAndArrayOptions } from './scriptable'; -export interface ScriptableContext { +export interface ScriptableContext { active: boolean; chart: Chart; dataIndex: number; @@ -535,7 +535,7 @@ export type UpdateMode = keyof typeof UpdateModeEnum; export class DatasetController< TElement extends Element = Element, TDatasetElement extends Element = Element, - TParsedData = unknown, + TParsedData extends unknown = unknown, > { constructor(chart: Chart, datasetIndex: number); @@ -749,7 +749,7 @@ export const layouts: { }; // TParsedData defaults to unknown here since plugins can work for any chart type -export interface Plugin extends ExtendedPlugin { +export interface Plugin extends ExtendedPlugin { id: string; /** @@ -1354,7 +1354,7 @@ export interface HoverInteractionOptions extends CoreInteractionOptions { onHover(event: ChartEvent, elements: ActiveElement[], chart: Chart): void; } -export interface CoreChartOptions extends ParsingOptions, AnimationOptions { +export interface CoreChartOptions extends ParsingOptions, AnimationOptions { datasets: AnimationOptions; @@ -1476,7 +1476,7 @@ export type EasingFunction = | 'easeOutBounce' | 'easeInOutBounce'; -export type AnimationSpec = { +export type AnimationSpec = { /** * The number of milliseconds an animation takes. * @default 1000 @@ -1507,7 +1507,7 @@ export type AnimationSpec = { loop: Scriptable>; } -export type AnimationsSpec = { +export type AnimationsSpec = { [name: string]: false | AnimationSpec & { properties: string[]; @@ -1529,16 +1529,16 @@ export type AnimationsSpec = { } } -export type TransitionSpec = { +export type TransitionSpec = { animation: AnimationSpec; animations: AnimationsSpec; } -export type TransitionsSpec = { +export type TransitionsSpec = { [mode: string]: TransitionSpec } -export type AnimationOptions = { +export type AnimationOptions = { animation: false | AnimationSpec & { /** * Callback called on each step of an animation. @@ -2215,7 +2215,7 @@ export interface TitleOptions { export type TooltipAlignment = 'start' | 'center' | 'end'; -export interface TooltipModel { +export interface TooltipModel { // The items that we are rendering in the tooltip. See Tooltip Item Interface section dataPoints: TooltipItem[]; @@ -2273,7 +2273,7 @@ export const Tooltip: Plugin & { setActiveElements(active: ActiveDataPoint[], eventPosition: { x: number, y: number }): void; }; -export interface TooltipCallbacks { +export interface TooltipCallbacks { beforeTitle(this: TooltipModel, tooltipItems: TooltipItem[]): string | string[]; title(this: TooltipModel, tooltipItems: TooltipItem[]): string | string[]; afterTitle(this: TooltipModel, tooltipItems: TooltipItem[]): string | string[]; @@ -2294,7 +2294,7 @@ export interface TooltipCallbacks { afterFooter(this: TooltipModel, tooltipItems: TooltipItem[]): string | string[]; } -export interface ExtendedPlugin { +export interface ExtendedPlugin { /** * @desc Called before drawing the `tooltip`. If any plugin returns `false`, * the tooltip drawing is cancelled until another `render` is triggered. @@ -2315,7 +2315,7 @@ export interface ExtendedPlugin { */ afterTooltipDraw?(chart: Chart, args: { tooltip: TooltipModel }, options: O): void; } -export interface TooltipOptions extends CoreInteractionOptions { +export interface TooltipOptions extends CoreInteractionOptions { /** * Are on-canvas tooltips enabled? * @default true @@ -2489,7 +2489,7 @@ export interface TooltipOptions extends CoreInteractionOptions { callbacks: TooltipCallbacks; } -export interface TooltipItem { +export interface TooltipItem { /** * The chart the tooltip is being shown on */ @@ -2536,14 +2536,14 @@ export interface TooltipItem { element: Element; } -export interface PluginOptionsByType { +export interface PluginOptionsByType { decimation: DecimationOptions; filler: FillerOptions; legend: LegendOptions; title: TitleOptions; tooltip: TooltipOptions; } -export interface PluginChartOptions { +export interface PluginChartOptions { plugins: Partial>; } @@ -3243,7 +3243,7 @@ export type ScaleChartOptions = { }; }; -export type ChartOptions = DeepPartial< +export type ChartOptions = DeepPartial< CoreChartOptions & ElementChartOptions & PluginChartOptions & From 7c8956452a8612a872e143402996faf44e39f814 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Mon, 22 Feb 2021 18:56:33 -0500 Subject: [PATCH 4/5] Add simplified test --- types/tests/test_specify_types_chart_constructor.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/tests/test_specify_types_chart_constructor.ts b/types/tests/test_specify_types_chart_constructor.ts index d0a036ee905..1c450787eb9 100644 --- a/types/tests/test_specify_types_chart_constructor.ts +++ b/types/tests/test_specify_types_chart_constructor.ts @@ -1,6 +1,6 @@ -import { Chart } from '../index.esm'; +import { Chart, CartesianParsedData } from '../index.esm'; -const chart = new Chart<'scatter', number[], string>('id', { +const chart = new Chart('id', { type: 'scatter', data: { labels: [], @@ -10,9 +10,9 @@ const chart = new Chart<'scatter', number[], string>('id', { }); interface Context { - chart: Chart<'scatter'|'bar', unknown[], unknown, unknown>; + chart: Chart; } const ctx: Context = { chart: chart -}; \ No newline at end of file +}; From 1cfab4703faf9c531beabbb7f317541593a736f7 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Wed, 24 Feb 2021 18:17:32 -0500 Subject: [PATCH 5/5] Eliminate options issue with TParsedData --- types/index.esm.d.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index 92cbbc88737..e6ba1b5b673 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -444,13 +444,12 @@ export declare class Chart< TType extends ChartType = ChartType, TData extends unknown[] = DefaultDataPoint, TLabel = unknown, - TParsedData extends unknown = ParsedDataType > { readonly platform: BasePlatform; readonly id: string; readonly canvas: HTMLCanvasElement; readonly ctx: CanvasRenderingContext2D; - readonly config: ChartConfiguration + readonly config: ChartConfiguration readonly width: number; readonly height: number; readonly aspectRatio: number; @@ -463,7 +462,7 @@ export declare class Chart< data: ChartData; options: ChartOptions; - constructor(item: ChartItem, config: ChartConfiguration); + constructor(item: ChartItem, config: ChartConfiguration); clear(): this; stop(): this; @@ -3243,10 +3242,10 @@ export type ScaleChartOptions = { }; }; -export type ChartOptions = DeepPartial< - CoreChartOptions & +export type ChartOptions = DeepPartial< + CoreChartOptions & ElementChartOptions & - PluginChartOptions & + PluginChartOptions & DatasetChartOptions & ScaleChartOptions & ChartTypeRegistry[TType]['chartOptions'] @@ -3283,10 +3282,9 @@ export interface ChartConfiguration< TType extends ChartType = ChartType, TData extends unknown[] = DefaultDataPoint, TLabel = unknown, - TParsedData extends unknown = ParsedDataType, > { type: TType; data: ChartData; - options?: ChartOptions; + options?: ChartOptions; plugins?: Plugin[]; }