]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Improve types (#7767)
authorxr0master <xr0master@gmail.com>
Wed, 9 Sep 2020 01:27:08 +0000 (04:27 +0300)
committerGitHub <noreply@github.com>
Wed, 9 Sep 2020 01:27:08 +0000 (21:27 -0400)
* interfaces.d: added plugins for the chart configuration
* index.d: allow to set data
* index.d: improve type for the update method
* interfaces.d: improve type of generic chart type
* interfaces.d: alphabetical sort
* index.d: set fields in alphabetical sort
* interfaces.d: remove custom type (string)
* core/index.d: import "chart type" type
* interfaces.d: added extends for TYPE
* interfaces.d: move chart types to enum
* core/index.d: move the update mode to enum

Co-authored-by: Sergey Khomushin <sergey@placer.io>
types/core/index.d.ts
types/interfaces.d.ts

index 10d063e28107c3184bd6d41438b02fd3da30957b..1739fcde71cf2d245a524016107c1b03e643044e 100644 (file)
@@ -10,7 +10,7 @@ import {
   TimeUnit,
   IEvent,
 } from './interfaces';
-import { IChartDataset, IChartConfiguration, ConfigurationOptions, ConfigurationData } from '../interfaces';
+import { IChartDataset, IChartConfiguration, ConfigurationOptions, ConfigurationData, IChartType } from '../interfaces';
 
 export interface IDateAdapter {
   /**
@@ -235,7 +235,7 @@ export interface IParsingOptions {
 export interface Chart<
   T = unknown,
   L = string,
-  C extends IChartConfiguration<string, T, L> = IChartConfiguration<string, T, L>
+  C extends IChartConfiguration<IChartType, T, L> = IChartConfiguration<IChartType, T, L>
 > {
   readonly platform: BasePlatform;
   readonly id: string;
@@ -248,11 +248,11 @@ export interface Chart<
   readonly boxes: ILayoutItem[];
   readonly currentDevicePixelRatio: number;
   readonly chartArea: IChartArea;
-  readonly data: ConfigurationData<C>;
   readonly scales: { [key: string]: Scale };
   readonly scale: Scale | undefined;
   readonly attached: boolean;
 
+  data: ConfigurationData<C>;
   options: ConfigurationOptions<C>;
 
   clear(): this;
@@ -263,7 +263,7 @@ export interface Chart<
   buildOrUpdateScales(): void;
   buildOrUpdateControllers(): void;
   reset(): void;
-  update(mode?: string): void;
+  update(mode?: UpdateMode): void;
   render(): void;
   draw(): void;
 
@@ -301,7 +301,7 @@ export declare type ChartItem =
 
 export const Chart: {
   prototype: Chart;
-  new <T = unknown, L = string, C extends IChartConfiguration<string, T, L> = IChartConfiguration<string, T, L>>(
+  new <T = unknown, L = string, C extends IChartConfiguration<IChartType, T, L> = IChartConfiguration<IChartType, T, L>>(
     item: ChartItem,
     config: C
   ): Chart<T, L, C>;
@@ -313,7 +313,17 @@ export const Chart: {
   unregister(...items: IChartComponentLike[]): void;
 };
 
-export type UpdateMode = 'resize' | 'reset' | 'none' | 'hide' | 'show' | 'normal' | 'active' | undefined;
+export enum UpdateModeEnum {
+  resize = 'resize',
+  reset = 'reset',
+  none = 'none',
+  hide = 'hide',
+  show = 'show',
+  normal = 'normal',
+  active = 'active'
+}
+
+export type UpdateMode = keyof typeof UpdateModeEnum;
 
 export class DatasetController<E extends Element = Element, DSE extends Element = Element> {
   constructor(chart: Chart, datasetIndex: number);
index d9d9d99a7a8135c9b34fd06d496ba1da455f42cb..9aa1e750edb8dc0106b62ed1a6eef34ade9c5a92 100644 (file)
@@ -28,7 +28,7 @@ import {
   ILegendChartOptions,
   ITitleChartOptions,
 } from './plugins';
-import { IChartAnimationOptions, IParsingOptions } from './core';
+import { IChartAnimationOptions, IParsingOptions, IPlugin } from './core';
 import { IScaleChartOptions } from './scales';
 
 export type DeepPartial<T> = T extends {}
@@ -71,8 +71,21 @@ export type IChartOptions<O = {}> = DeepPartial<
     O
 >;
 
+export enum ChartTypeEnum {
+  bar = 'bar',
+  bubble = 'bubble',
+  doughnut = 'doughnut',
+  line = 'line',
+  pie = 'pie',
+  polarArea = 'polarArea',
+  radar = 'radar',
+  scatter = 'scatter',
+}
+
+export type IChartType = keyof typeof ChartTypeEnum;
+
 export interface IChartConfiguration<
-  TYPE = string,
+  TYPE extends IChartType = IChartType,
   T = unknown,
   L = string,
   DS extends IChartDataset<T> = IChartDataset<T>,
@@ -81,6 +94,7 @@ export interface IChartConfiguration<
   type: TYPE;
   data: IChartData<T, L, DS>;
   options?: IChartOptions<O>;
+  plugins?: IPlugin[];
 }
 
 export type IBarControllerConfiguration<T = number, L = string> = IChartConfiguration<
@@ -139,10 +153,10 @@ export type IRadarControllerConfiguration<T = number, L = string> = IChartConfig
   IRadarControllerChartOptions
 >;
 
-export type ConfigurationOptions<O> = O extends IChartConfiguration<unknown, unknown, unknown, infer O> ? O : never;
-export type ConfigurationData<O> = O extends IChartConfiguration<unknown, infer T, infer L, infer DS, unknown>
+export type ConfigurationOptions<O> = O extends IChartConfiguration<IChartType, unknown, unknown, infer O> ? O : never;
+export type ConfigurationData<O> = O extends IChartConfiguration<IChartType, infer T, infer L, infer DS, unknown>
   ? IChartData<T, L, DS>
   : never;
-export type ConfigurationDataset<O> = O extends IChartConfiguration<unknown, unknown, unknown, infer DS, unknown>
+export type ConfigurationDataset<O> = O extends IChartConfiguration<IChartType, unknown, unknown, infer DS, unknown>
   ? DS
   : never;