Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3-beta][types] Improve types #7767

Merged
merged 11 commits into from
Sep 9, 2020
22 changes: 16 additions & 6 deletions types/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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;
Expand All @@ -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>;
xr0master marked this conversation as resolved.
Show resolved Hide resolved
options: ConfigurationOptions<C>;

clear(): this;
Expand All @@ -263,7 +263,7 @@ export interface Chart<
buildOrUpdateScales(): void;
buildOrUpdateControllers(): void;
reset(): void;
update(mode?: string): void;
update(mode?: UpdateMode): void;
kurkle marked this conversation as resolved.
Show resolved Hide resolved
render(): void;
draw(): void;

Expand Down Expand Up @@ -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>;
Expand All @@ -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);
Expand Down
24 changes: 19 additions & 5 deletions types/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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,
kurkle marked this conversation as resolved.
Show resolved Hide resolved
T = unknown,
L = string,
DS extends IChartDataset<T> = IChartDataset<T>,
Expand All @@ -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<
Expand Down Expand Up @@ -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;