From 90758950add3ae01148e90be7c59eedb5fdc2ccc Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Sat, 6 Feb 2021 02:59:55 +0200 Subject: [PATCH] Update ScriptableContext, reduce warnings (#8390) * Update ScriptableContext, reduce warnings * _parsed is unknown[] * Add defaults * static defaults. * review update * more cancelables --- types/animation.d.ts | 11 +-- types/basic.d.ts | 3 + types/helpers/helpers.core.d.ts | 22 ++--- types/index.esm.d.ts | 151 +++++++++++++++++--------------- 4 files changed, 100 insertions(+), 87 deletions(-) create mode 100644 types/basic.d.ts diff --git a/types/animation.d.ts b/types/animation.d.ts index e383e829f..9f78e06c9 100644 --- a/types/animation.d.ts +++ b/types/animation.d.ts @@ -1,9 +1,10 @@ import { Chart } from './index.esm'; +import { AnyObject } from './basic'; export class Animation { - constructor(cfg: any, target: any, prop: string, to?: any); + constructor(cfg: AnyObject, target: AnyObject, prop: string, to?: unknown); active(): boolean; - update(cfg: any, to: any, date: number): void; + update(cfg: AnyObject, to: unknown, date: number): void; cancel(): void; tick(date: number): void; } @@ -25,7 +26,7 @@ export class Animator { } export class Animations { - constructor(chart: Chart, animations: {}); - configure(animations: {}): void; - update(target: any, values: any): undefined | boolean; + constructor(chart: Chart, animations: AnyObject); + configure(animations: AnyObject): void; + update(target: AnyObject, values: AnyObject): undefined | boolean; } diff --git a/types/basic.d.ts b/types/basic.d.ts new file mode 100644 index 000000000..1692c9cb3 --- /dev/null +++ b/types/basic.d.ts @@ -0,0 +1,3 @@ + +export type AnyObject = Record; +export type EmptyObject = Record; diff --git a/types/helpers/helpers.core.d.ts b/types/helpers/helpers.core.d.ts index d96f84b75..48beb6b42 100644 --- a/types/helpers/helpers.core.d.ts +++ b/types/helpers/helpers.core.d.ts @@ -1,3 +1,5 @@ +import { AnyObject } from '../basic'; + /** * An empty function that can be used, for example, for optional callback. */ @@ -15,27 +17,27 @@ export function uid(): number; * @returns {boolean} * @since 2.7.0 */ -export function isNullOrUndef(value: any): value is null | undefined; +export function isNullOrUndef(value: unknown): value is null | undefined; /** * Returns true if `value` is an array (including typed arrays), else returns false. * @param {*} value - The value to test. * @returns {boolean} * @function */ -export function isArray(value: any): value is ArrayLike; +export function isArray(value: unknown): value is ArrayLike; /** * Returns true if `value` is an object (excluding null), else returns false. * @param {*} value - The value to test. * @returns {boolean} * @since 2.7.0 */ -export function isObject(value: any): value is object; +export function isObject(value: unknown): value is AnyObject; /** * Returns true if `value` is a finite number, else returns false * @param {*} value - The value to test. * @returns {boolean} */ -export function isFinite(value: any): value is number; +export function isFinite(value: unknown): value is number; /** * Returns `value` if defined, else returns `defaultValue`. * @param {*} value - The value to return if defined. @@ -51,9 +53,9 @@ export function valueOrDefault(value: T | undefined, defaultValue: T): T; * @param [thisArg] - The value of `this` provided for the call to `fn`. * @returns {*} */ -export function callback R, TA, R>( +export function callback R, TA, R>( fn: T | undefined, - args: any[], + args: unknown[], thisArg?: TA ): R | undefined; @@ -95,7 +97,7 @@ export function each( export function clone(source: T): T; export interface MergeOptions { - merger?: (key: string, target: any, source: any, options: any) => any; + merger?: (key: string, target: AnyObject, source: AnyObject, options: AnyObject) => AnyObject; } /** * Recursively deep copies `source` properties into `target` with the given `options`. @@ -116,7 +118,7 @@ export function merge( source: [S1, S2, S3, S4], options?: MergeOptions ): T & S1 & S2 & S3 & S4; -export function merge(target: T, source: any[], options?: MergeOptions): any; +export function merge(target: T, source: AnyObject[], options?: MergeOptions): AnyObject; /** * Recursively deep copies `source` properties into `target` *only* if not defined in target. @@ -131,6 +133,6 @@ export function mergeIf(target: T, source: [S1]): T & S1; export function mergeIf(target: T, source: [S1, S2]): T & S1 & S2; export function mergeIf(target: T, source: [S1, S2, S3]): T & S1 & S2 & S3; export function mergeIf(target: T, source: [S1, S2, S3, S4]): T & S1 & S2 & S3 & S4; -export function mergeIf(target: T, source: any[]): any; +export function mergeIf(target: T, source: AnyObject[]): AnyObject; -export function resolveObjectKey(obj: any, key: string): any; +export function resolveObjectKey(obj: AnyObject, key: string): AnyObject; diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index c398b0f5f..200ec1379 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -14,6 +14,7 @@ import { TimeUnit } from './adapters'; import { AnimationEvent } from './animation'; +import { AnyObject, EmptyObject } from './basic'; import { Color } from './color'; import { Element } from './element'; import { ChartArea, Point } from './geometric'; @@ -39,7 +40,8 @@ export { export interface ScriptableContext { chart: Chart; - dataPoint: any; + parsed: unknown[]; + raw: unknown[]; dataIndex: number; dataset: ChartDataset; datasetIndex: number; @@ -140,7 +142,7 @@ export interface BarControllerChartOptions { skipNull?: boolean; } -export interface BarController extends DatasetController {} +export type BarController = DatasetController export const BarController: ChartComponent & { prototype: BarController; new (chart: Chart, datasetIndex: number): BarController; @@ -168,7 +170,7 @@ export interface BubbleDataPoint { r: number; } -export interface BubbleController extends DatasetController {} +export type BubbleController = DatasetController export const BubbleController: ChartComponent & { prototype: BubbleController; new (chart: Chart, datasetIndex: number): BubbleController; @@ -211,7 +213,7 @@ export interface LineControllerChartOptions { showLine: boolean; } -export interface LineController extends DatasetController {} +export type LineController = DatasetController export const LineController: ChartComponent & { prototype: LineController; new (chart: Chart, datasetIndex: number): LineController; @@ -226,7 +228,7 @@ export interface ScatterDataPoint { export type ScatterControllerChartOptions = LineControllerChartOptions; -export interface ScatterController extends LineController {} +export type ScatterController = LineController export const ScatterController: ChartComponent & { prototype: ScatterController; new (chart: Chart, datasetIndex: number): ScatterController; @@ -316,7 +318,7 @@ export type PieAnimationOptions = DoughnutAnimationOptions; export type PieDataPoint = DoughnutDataPoint; -export interface PieController extends DoughnutController {} +export type PieController = DoughnutController export const PieController: ChartComponent & { prototype: PieController; new (chart: Chart, datasetIndex: number): PieController; @@ -378,7 +380,7 @@ export interface RadarControllerDatasetOptions export type RadarControllerChartOptions = LineControllerChartOptions; -export interface RadarController extends DatasetController {} +export type RadarController = DatasetController export const RadarController: ChartComponent & { prototype: RadarController; new (chart: Chart, datasetIndex: number): RadarController; @@ -415,7 +417,7 @@ export interface ChartMeta, data: any[], start: number, count: number): any[]; - protected parseArrayData(meta: ChartMeta, data: any[], start: number, count: number): any[]; - protected parseObjectData(meta: ChartMeta, data: any[], start: number, count: number): any[]; - protected getParsed(index: number): any; - protected applyStack(scale: Scale, parsed: any[]): number; + protected parsePrimitiveData(meta: ChartMeta, data: AnyObject[], start: number, count: number): AnyObject[]; + protected parseArrayData(meta: ChartMeta, data: AnyObject[], start: number, count: number): AnyObject[]; + protected parseObjectData(meta: ChartMeta, data: AnyObject[], start: number, count: number): AnyObject[]; + protected getParsed(index: number): AnyObject; + protected applyStack(scale: Scale, parsed: unknown[]): number; protected updateRangeFromParsed( range: { min: number; max: number }, scale: Scale, - parsed: any[], + parsed: unknown[], stack: boolean ): void; protected getMinMax(scale: Scale, canStack?: boolean): { min: number; max: number }; @@ -593,7 +596,6 @@ export interface DatasetControllerChartComponent extends ChartComponent { }; } -export type AnyObject = Record; export interface Defaults extends CoreChartOptions, ElementChartOptions, PluginChartOptions { controllers: { [key in ChartType]: DeepPartial< @@ -729,7 +731,7 @@ export const layouts: { update(chart: Chart, width: number, height: number): void; }; -export interface Plugin extends ExtendedPlugin { +export interface Plugin extends ExtendedPlugin { id: string; /** @@ -739,7 +741,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since 3.0.0 */ - install?(chart: Chart, args: {}, options: O): void; + install?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called when a plugin is starting. This happens when chart is created or plugin is enabled. * @param {Chart} chart - The chart instance. @@ -747,7 +749,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since 3.0.0 */ - start?(chart: Chart, args: {}, options: O): void; + start?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called when a plugin stopping. This happens when chart is destroyed or plugin is disabled. * @param {Chart} chart - The chart instance. @@ -755,21 +757,21 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since 3.0.0 */ - stop?(chart: Chart, args: {}, options: O): void; + stop?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before initializing `chart`. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - beforeInit?(chart: Chart, args: {}, options: O): void; + beforeInit?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called after `chart` has been initialized and before the first update. * @param {Chart} chart - The chart instance. * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - afterInit?(chart: Chart, args: {}, options: O): void; + afterInit?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before updating `chart`. If any plugin returns `false`, the update * is cancelled (and thus subsequent render(s)) until another `update` is triggered. @@ -779,7 +781,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart update. */ - beforeUpdate?(chart: Chart, args: { mode: UpdateMode }, options: O): boolean | void; + beforeUpdate?(chart: Chart, args: { mode: UpdateMode, cancelable: true }, options: O): boolean | void; /** * @desc Called after `chart` has been updated and before rendering. Note that this * hook will not be called if the chart update has been previously cancelled. @@ -796,7 +798,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - beforeElementsUpdate?(chart: Chart, args: {}, options: O): void; + beforeElementsUpdate?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called during chart reset * @param {Chart} chart - The chart instance. @@ -804,7 +806,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since version 3.0.0 */ - reset?(chart: Chart, args: {}, options: O): void; + reset?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before updating the `chart` datasets. If any plugin returns `false`, * the datasets update is cancelled until another `update` is triggered. @@ -825,7 +827,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since version 2.1.5 */ - afterDatasetsUpdate?(chart: Chart, args: { mode: UpdateMode }, options: O): void; + afterDatasetsUpdate?(chart: Chart, args: { mode: UpdateMode, cancelable: true }, options: O): void; /** * @desc Called before updating the `chart` dataset at the given `args.index`. If any plugin * returns `false`, the datasets update is cancelled until another `update` is triggered. @@ -837,7 +839,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart datasets drawing. */ - beforeDatasetUpdate?(chart: Chart, args: { index: number; meta: ChartMeta, mode: UpdateMode }, options: O): boolean | void; + beforeDatasetUpdate?(chart: Chart, args: { index: number; meta: ChartMeta, mode: UpdateMode, cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` datasets at the given `args.index` has been updated. Note * that this hook will not be called if the datasets update has been previously cancelled. @@ -848,7 +850,7 @@ export interface Plugin extends ExtendedPlugin { * @param {UpdateMode} args.mode - The update mode. * @param {object} options - The plugin options. */ - afterDatasetUpdate?(chart: Chart, args: { index: number; meta: ChartMeta, mode: UpdateMode }, options: O): void; + afterDatasetUpdate?(chart: Chart, args: { index: number; meta: ChartMeta, mode: UpdateMode, cancelable: false }, options: O): void; /** * @desc Called before laying out `chart`. If any plugin returns `false`, * the layout update is cancelled until another `update` is triggered. @@ -857,7 +859,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart layout. */ - beforeLayout?(chart: Chart, args: {}, options: O): boolean | void; + beforeLayout?(chart: Chart, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called before scale data limits are calculated. This hook is called separately for each scale in the chart. * @param {Chart} chart - The chart instance. @@ -897,7 +899,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - afterLayout?(chart: Chart, args: {}, options: O): void; + afterLayout?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before rendering `chart`. If any plugin returns `false`, * the rendering is cancelled until another `render` is triggered. @@ -906,7 +908,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart rendering. */ - beforeRender?(chart: Chart, args: {}, options: O): boolean | void; + beforeRender?(chart: Chart, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` has been fully rendered (and animation completed). Note * that this hook will not be called if the rendering has been previously cancelled. @@ -914,7 +916,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - afterRender?(chart: Chart, args: {}, options: O): void; + afterRender?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before drawing `chart` at every animation frame. If any plugin returns `false`, * the frame drawing is cancelled untilanother `render` is triggered. @@ -923,7 +925,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart drawing. */ - beforeDraw?(chart: Chart, args: {}, options: O): boolean | void; + beforeDraw?(chart: Chart, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` has been drawn. Note that this hook will not be called * if the drawing has been previously cancelled. @@ -931,7 +933,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - afterDraw?(chart: Chart, args: {}, options: O): void; + afterDraw?(chart: Chart, args: EmptyObject, options: O): void; /** * @desc Called before drawing the `chart` datasets. If any plugin returns `false`, * the datasets drawing is cancelled until another `render` is triggered. @@ -940,7 +942,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @returns {boolean} `false` to cancel the chart datasets drawing. */ - beforeDatasetsDraw?(chart: Chart, args: {}, options: O): boolean | void; + beforeDatasetsDraw?(chart: Chart, args: { cancelable: true }, options: O): boolean | void; /** * @desc Called after the `chart` datasets have been drawn. Note that this hook * will not be called if the datasets drawing has been previously cancelled. @@ -948,7 +950,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - afterDatasetsDraw?(chart: Chart, args: {}, options: O): void; + afterDatasetsDraw?(chart: Chart, args: EmptyObject, options: O, cancelable: false): void; /** * @desc Called before drawing the `chart` dataset at the given `args.index` (datasets * are drawn in the reverse order). If any plugin returns `false`, the datasets drawing @@ -981,7 +983,7 @@ export interface Plugin extends ExtendedPlugin { * @param {boolean} args.replay - True if this event is replayed from `Chart.update` * @param {object} options - The plugin options. */ - beforeEvent?(chart: Chart, args: { event: ChartEvent, replay: boolean }, options: O): boolean | void; + beforeEvent?(chart: Chart, args: { event: ChartEvent, replay: boolean, cancelable: true }, options: O): boolean | void; /** * @desc Called after the `event` has been consumed. Note that this hook * will not be called if the `event` has been previously discarded. @@ -992,7 +994,7 @@ export interface Plugin extends ExtendedPlugin { * @param {boolean} [args.changed] - Set to true if the plugin needs a render. Should only be changed to true, because this args object is passed through all plugins. * @param {object} options - The plugin options. */ - afterEvent?(chart: Chart, args: { event: ChartEvent, replay: boolean, changed?: boolean }, options: O): void; + afterEvent?(chart: Chart, args: { event: ChartEvent, replay: boolean, changed?: boolean, cancelable: false }, options: O): void; /** * @desc Called after the chart as been resized. * @param {Chart} chart - The chart instance. @@ -1007,7 +1009,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} args - The call arguments. * @param {object} options - The plugin options. */ - destroy?(chart: Chart, args: {}, options: O): void; + destroy?(chart: Chart, args: EmptyObject, options: O): void; /** * Called after chart is destroyed on all plugins that were installed for that chart. This hook is also invoked for disabled plugins (options === false). * @param {Chart} chart - The chart instance. @@ -1015,7 +1017,7 @@ export interface Plugin extends ExtendedPlugin { * @param {object} options - The plugin options. * @since 3.0.0 */ - uninstall?(chart: Chart, args: {}, options: O): void; + uninstall?(chart: Chart, args: EmptyObject, options: O): void; } export declare type ChartComponentLike = ChartComponent | ChartComponent[] | { [key: string]: ChartComponent }; @@ -1202,7 +1204,7 @@ export interface Scale extends El getBasePixel(): number; init(options: O): void; - parse(raw: any, index: number): any; + parse(raw: unknown, index: number): unknown; getUserBounds(): { min: number; max: number; minDefined: boolean; maxDefined: boolean }; getMinMax(canStack: boolean): { min: number; max: number }; getTicks(): Tick[]; @@ -1233,7 +1235,7 @@ export interface Scale extends El } export const Scale: { prototype: Scale; - new (cfg: any): Scale; + new (cfg: AnyObject): Scale; }; export interface ScriptableScaleContext { @@ -1250,7 +1252,7 @@ export const Ticks: { * @param value the value to display * @return {string|string[]} the label to display */ - values(value: any): string | string[]; + values(value: unknown): string | string[]; /** * Formatter for numeric ticks * @param tickValue the value to be formatted @@ -1300,7 +1302,7 @@ export interface ChartEvent { } export interface ChartComponent { id: string; - defaults?: any; + defaults?: AnyObject; defaultRoutes?: { [property: string]: string }; beforeRegister?(): void; @@ -1618,7 +1620,7 @@ export interface ArcElement string; + callback: (tickValue: unknown, index: number, ticks: Tick[]) => string; /** * If true, show tick labels. * @default true @@ -2732,10 +2739,10 @@ export type CategoryScaleOptions = CartesianScaleOptions & { labels: string[] | string[][]; }; -export interface CategoryScale extends Scale {} +export type CategoryScale = Scale export const CategoryScale: ChartComponent & { prototype: CategoryScale; - new (cfg: any): CategoryScale; + new (cfg: AnyObject): CategoryScale; }; export type LinearScaleOptions = CartesianScaleOptions & { @@ -2781,10 +2788,10 @@ export type LinearScaleOptions = CartesianScaleOptions & { }; }; -export interface LinearScale extends Scale {} +export type LinearScale = Scale export const LinearScale: ChartComponent & { prototype: LinearScale; - new (cfg: any): LinearScale; + new (cfg: AnyObject): LinearScale; }; export type LogarithmicScaleOptions = CartesianScaleOptions & { @@ -2808,10 +2815,10 @@ export type LogarithmicScaleOptions = CartesianScaleOptions & { }; }; -export interface LogarithmicScale extends Scale {} +export type LogarithmicScale = Scale export const LogarithmicScale: ChartComponent & { prototype: LogarithmicScale; - new (cfg: any): LogarithmicScale; + new (cfg: AnyObject): LogarithmicScale; }; export type TimeScaleOptions = CartesianScaleOptions & { @@ -2829,7 +2836,7 @@ export type TimeScaleOptions = CartesianScaleOptions & { * options for creating a new adapter instance */ adapters: { - date: any; + date: unknown; }; time: { @@ -2837,7 +2844,7 @@ export type TimeScaleOptions = CartesianScaleOptions & { * Custom parser for dates. * @see https://www.chartjs.org/docs/next/axes/cartesian/time#parser */ - parser: string | ((v: any) => number); + parser: string | ((v: unknown) => number); /** * If defined, dates will be rounded to the start of this unit. See Time Units below for the allowed units. */ @@ -2900,13 +2907,13 @@ export interface TimeScale extend export const TimeScale: ChartComponent & { prototype: TimeScale; - new (cfg: any): TimeScale; + new (cfg: AnyObject): TimeScale; }; -export interface TimeSeriesScale extends TimeScale {} +export type TimeSeriesScale = TimeScale export const TimeSeriesScale: ChartComponent & { prototype: TimeSeriesScale; - new (cfg: any): TimeSeriesScale; + new (cfg: AnyObject): TimeSeriesScale; }; export type RadialLinearScaleOptions = CoreScaleOptions & { @@ -3046,7 +3053,7 @@ export interface RadialLinearScale(cfg: any): RadialLinearScale; + new (cfg: AnyObject): RadialLinearScale; }; // DeepPartial implementation taken from the utility-types NPM package, which is @@ -3059,7 +3066,7 @@ export type DeepPartial = T extends Function : T extends object ? _DeepPartialObject : T | undefined; - interface _DeepPartialArray extends Array> {} + type _DeepPartialArray = Array> type _DeepPartialObject = { [P in keyof T]?: DeepPartial }; export type DistributiveArray = T extends unknown ? T[] : never @@ -3113,7 +3120,7 @@ export interface ChartTypeRegistry { scales: keyof CartesianScaleTypeRegistry; }; bubble: { - chartOptions: {}; + chartOptions: EmptyObject; datasetOptions: BubbleControllerDatasetOptions; defaultDataPoint: BubbleDataPoint; scales: keyof CartesianScaleTypeRegistry;