From 1ec6f1da71493792fc2a9ea2de92f2370d4fefad Mon Sep 17 00:00:00 2001 From: xr0master Date: Wed, 9 Sep 2020 04:27:08 +0300 Subject: [PATCH] Improve types (#7767) * 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 --- types/core/index.d.ts | 22 ++++++++++++++++------ types/interfaces.d.ts | 24 +++++++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/types/core/index.d.ts b/types/core/index.d.ts index 10d063e28..1739fcde7 100644 --- a/types/core/index.d.ts +++ b/types/core/index.d.ts @@ -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 = IChartConfiguration + C extends IChartConfiguration = IChartConfiguration > { 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; readonly scales: { [key: string]: Scale }; readonly scale: Scale | undefined; readonly attached: boolean; + data: ConfigurationData; options: ConfigurationOptions; 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 = IChartConfiguration>( + new = IChartConfiguration>( item: ChartItem, config: C ): Chart; @@ -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 { constructor(chart: Chart, datasetIndex: number); diff --git a/types/interfaces.d.ts b/types/interfaces.d.ts index d9d9d99a7..9aa1e750e 100644 --- a/types/interfaces.d.ts +++ b/types/interfaces.d.ts @@ -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 extends {} @@ -71,8 +71,21 @@ export type IChartOptions = 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 = IChartDataset, @@ -81,6 +94,7 @@ export interface IChartConfiguration< type: TYPE; data: IChartData; options?: IChartOptions; + plugins?: IPlugin[]; } export type IBarControllerConfiguration = IChartConfiguration< @@ -139,10 +153,10 @@ export type IRadarControllerConfiguration = IChartConfig IRadarControllerChartOptions >; -export type ConfigurationOptions = O extends IChartConfiguration ? O : never; -export type ConfigurationData = O extends IChartConfiguration +export type ConfigurationOptions = O extends IChartConfiguration ? O : never; +export type ConfigurationData = O extends IChartConfiguration ? IChartData : never; -export type ConfigurationDataset = O extends IChartConfiguration +export type ConfigurationDataset = O extends IChartConfiguration ? DS : never;