]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Make type-tests strict (#8717) v3.0.0-rc.4
authorJukka Kurkela <jukka.kurkela@gmail.com>
Thu, 25 Mar 2021 19:09:35 +0000 (21:09 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Mar 2021 19:09:35 +0000 (15:09 -0400)
tsconfig.json
types/index.esm.d.ts
types/tests/plugins/defaults.ts [new file with mode: 0644]
types/tests/scales/options.ts [new file with mode: 0644]
types/tests/tsconfig.json
types/utils.d.ts

index 1765c6eec63f51b503c18d1da8af9081fd93b8da..86c555b9c3c1284408a44626cfe1922ad0ace9e7 100644 (file)
@@ -20,5 +20,8 @@
   "include": [
     "./src/**/*.js",
     "./types"
+  ],
+  "exclude": [
+    "./types/tests"
   ]
 }
index f8e8609f8d854731c3d4a7e762af393834ad71fe..421c4ac8b85512437a2295aa8b9a7cad44409259 100644 (file)
@@ -633,14 +633,13 @@ export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptio
 }
 
 export type Overrides = {
-  [key in ChartType]: DeepPartial<
+  [key in ChartType]:
     CoreChartOptions<key> &
     ElementChartOptions &
     PluginChartOptions<key> &
     DatasetChartOptions<ChartType> &
     ScaleChartOptions<key> &
-    ChartTypeRegistry[key]['chartOptions']
-    >;
+    ChartTypeRegistry[key]['chartOptions'];
 }
 
 export const defaults: Defaults;
@@ -2561,7 +2560,7 @@ export interface PluginOptionsByType<TType extends ChartType> {
   tooltip: TooltipOptions<TType>;
 }
 export interface PluginChartOptions<TType extends ChartType> {
-  plugins: Partial<PluginOptionsByType<TType>>;
+  plugins: PluginOptionsByType<TType>;
 }
 
 export interface GridLineOptions {
@@ -3245,9 +3244,9 @@ export interface ChartTypeRegistry {
 
 export type ChartType = keyof ChartTypeRegistry;
 
-export type ScaleOptionsByType<TScale extends ScaleType = ScaleType> = DeepPartial<
+export type ScaleOptionsByType<TScale extends ScaleType = ScaleType> =
   { [key in ScaleType]: { type: key } & ScaleTypeRegistry[key]['options'] }[TScale]
->;
+;
 
 export type DatasetChartOptions<TType extends ChartType = ChartType> = {
   [key in TType]: {
diff --git a/types/tests/plugins/defaults.ts b/types/tests/plugins/defaults.ts
new file mode 100644 (file)
index 0000000..ceab8af
--- /dev/null
@@ -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 (file)
index 0000000..cf4f4dc
--- /dev/null
@@ -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'
+        }
+      }
+    }
+  }
+});
index 2c18b014b0fde34670df33519742316a388417ee..9bbed3c55d642f591880b6c3a2a3c12fbb82a27e 100644 (file)
@@ -3,6 +3,7 @@
     "target": "ES6",
     "moduleResolution": "Node",
     "alwaysStrict": true,
+    "strict": true,
     "noEmit": true
   },
   "include": [
index 32b4fc1d5add4bfd74a334af0f32303883eb8644..592aa63815bb003e5dcdff20eb8202d4a430b2a1 100644 (file)
@@ -12,7 +12,7 @@ export type DeepPartial<T> = T extends Function
   type _DeepPartialArray<T> = Array<DeepPartial<T>>
 type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
 
-export type DistributiveArray<T> = T extends unknown ? T[] : never
+export type DistributiveArray<T> = [T] extends [unknown] ? Array<T> : never
 
 // From https://stackoverflow.com/a/50375286
 export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;