From 499a71d4eb660f4b728f6d462dcd6c36db282057 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Thu, 25 Mar 2021 21:09:35 +0200 Subject: [PATCH] Make type-tests strict (#8717) --- tsconfig.json | 3 +++ types/index.esm.d.ts | 11 +++++------ types/tests/plugins/defaults.ts | 18 ++++++++++++++++++ types/tests/scales/options.ts | 32 ++++++++++++++++++++++++++++++++ types/tests/tsconfig.json | 1 + types/utils.d.ts | 2 +- 6 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 types/tests/plugins/defaults.ts create mode 100644 types/tests/scales/options.ts diff --git a/tsconfig.json b/tsconfig.json index 1765c6eec63..86c555b9c3c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,5 +20,8 @@ "include": [ "./src/**/*.js", "./types" + ], + "exclude": [ + "./types/tests" ] } diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index f8e8609f8d8..421c4ac8b85 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -633,14 +633,13 @@ export interface Defaults extends CoreChartOptions, ElementChartOptio } export type Overrides = { - [key in ChartType]: DeepPartial< + [key in ChartType]: CoreChartOptions & ElementChartOptions & PluginChartOptions & DatasetChartOptions & ScaleChartOptions & - ChartTypeRegistry[key]['chartOptions'] - >; + ChartTypeRegistry[key]['chartOptions']; } export const defaults: Defaults; @@ -2561,7 +2560,7 @@ export interface PluginOptionsByType { tooltip: TooltipOptions; } export interface PluginChartOptions { - plugins: Partial>; + plugins: PluginOptionsByType; } export interface GridLineOptions { @@ -3245,9 +3244,9 @@ export interface ChartTypeRegistry { export type ChartType = keyof ChartTypeRegistry; -export type ScaleOptionsByType = DeepPartial< +export type ScaleOptionsByType = { [key in ScaleType]: { type: key } & ScaleTypeRegistry[key]['options'] }[TScale] ->; +; export type DatasetChartOptions = { [key in TType]: { diff --git a/types/tests/plugins/defaults.ts b/types/tests/plugins/defaults.ts new file mode 100644 index 00000000000..ceab8af9188 --- /dev/null +++ b/types/tests/plugins/defaults.ts @@ -0,0 +1,18 @@ +import { defaults } from '../../index.esm'; + +// https://github.com/chartjs/Chart.js/issues/8711 +const original = defaults.plugins.legend.labels.generateLabels; + +defaults.plugins.legend.labels.generateLabels = function(chart) { + return [{ + datasetIndex: 0, + text: 'test' + }]; +}; + +// @ts-expect-error Type '{ text: string; }[]' is not assignable to type 'LegendItem[]'. +defaults.plugins.legend.labels.generateLabels = function(chart) { + return [{ + text: 'test' + }]; +}; diff --git a/types/tests/scales/options.ts b/types/tests/scales/options.ts new file mode 100644 index 00000000000..cf4f4dcb854 --- /dev/null +++ b/types/tests/scales/options.ts @@ -0,0 +1,32 @@ +import { Chart } from '../../index.esm'; + +const chart = new Chart('test', { + type: 'bar', + data: { + labels: ['a'], + datasets: [{ + data: [1], + }, { + type: 'line', + data: [{ x: 1, y: 1 }] + }] + }, + options: { + scales: { + x: { + type: 'time', + time: { + unit: 'year' + } + }, + x1: { + // @ts-expect-error Type '"linear"' is not assignable to type '"timeseries" | undefined'. + type: 'linear', + time: { + // @ts-expect-error Type 'string' is not assignable to type 'false | "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year" | undefined'. + unit: 'year' + } + } + } + } +}); diff --git a/types/tests/tsconfig.json b/types/tests/tsconfig.json index 2c18b014b0f..9bbed3c55d6 100644 --- a/types/tests/tsconfig.json +++ b/types/tests/tsconfig.json @@ -3,6 +3,7 @@ "target": "ES6", "moduleResolution": "Node", "alwaysStrict": true, + "strict": true, "noEmit": true }, "include": [ diff --git a/types/utils.d.ts b/types/utils.d.ts index 32b4fc1d5ad..592aa63815b 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -12,7 +12,7 @@ export type DeepPartial = T extends Function type _DeepPartialArray = Array> type _DeepPartialObject = { [P in keyof T]?: DeepPartial }; -export type DistributiveArray = T extends unknown ? T[] : never +export type DistributiveArray = [T] extends [unknown] ? Array : never // From https://stackoverflow.com/a/50375286 export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;