Skip to content

Commit 499a71d

Browse files
authored
Make type-tests strict (#8717)
1 parent 3671c01 commit 499a71d

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"include": [
2121
"./src/**/*.js",
2222
"./types"
23+
],
24+
"exclude": [
25+
"./types/tests"
2326
]
2427
}

types/index.esm.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,13 @@ export interface Defaults extends CoreChartOptions<ChartType>, ElementChartOptio
633633
}
634634

635635
export type Overrides = {
636-
[key in ChartType]: DeepPartial<
636+
[key in ChartType]:
637637
CoreChartOptions<key> &
638638
ElementChartOptions &
639639
PluginChartOptions<key> &
640640
DatasetChartOptions<ChartType> &
641641
ScaleChartOptions<key> &
642-
ChartTypeRegistry[key]['chartOptions']
643-
>;
642+
ChartTypeRegistry[key]['chartOptions'];
644643
}
645644

646645
export const defaults: Defaults;
@@ -2561,7 +2560,7 @@ export interface PluginOptionsByType<TType extends ChartType> {
25612560
tooltip: TooltipOptions<TType>;
25622561
}
25632562
export interface PluginChartOptions<TType extends ChartType> {
2564-
plugins: Partial<PluginOptionsByType<TType>>;
2563+
plugins: PluginOptionsByType<TType>;
25652564
}
25662565

25672566
export interface GridLineOptions {
@@ -3245,9 +3244,9 @@ export interface ChartTypeRegistry {
32453244

32463245
export type ChartType = keyof ChartTypeRegistry;
32473246

3248-
export type ScaleOptionsByType<TScale extends ScaleType = ScaleType> = DeepPartial<
3247+
export type ScaleOptionsByType<TScale extends ScaleType = ScaleType> =
32493248
{ [key in ScaleType]: { type: key } & ScaleTypeRegistry[key]['options'] }[TScale]
3250-
>;
3249+
;
32513250

32523251
export type DatasetChartOptions<TType extends ChartType = ChartType> = {
32533252
[key in TType]: {

types/tests/plugins/defaults.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defaults } from '../../index.esm';
2+
3+
// https://github.com/chartjs/Chart.js/issues/8711
4+
const original = defaults.plugins.legend.labels.generateLabels;
5+
6+
defaults.plugins.legend.labels.generateLabels = function(chart) {
7+
return [{
8+
datasetIndex: 0,
9+
text: 'test'
10+
}];
11+
};
12+
13+
// @ts-expect-error Type '{ text: string; }[]' is not assignable to type 'LegendItem[]'.
14+
defaults.plugins.legend.labels.generateLabels = function(chart) {
15+
return [{
16+
text: 'test'
17+
}];
18+
};

types/tests/scales/options.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Chart } from '../../index.esm';
2+
3+
const chart = new Chart('test', {
4+
type: 'bar',
5+
data: {
6+
labels: ['a'],
7+
datasets: [{
8+
data: [1],
9+
}, {
10+
type: 'line',
11+
data: [{ x: 1, y: 1 }]
12+
}]
13+
},
14+
options: {
15+
scales: {
16+
x: {
17+
type: 'time',
18+
time: {
19+
unit: 'year'
20+
}
21+
},
22+
x1: {
23+
// @ts-expect-error Type '"linear"' is not assignable to type '"timeseries" | undefined'.
24+
type: 'linear',
25+
time: {
26+
// @ts-expect-error Type 'string' is not assignable to type 'false | "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year" | undefined'.
27+
unit: 'year'
28+
}
29+
}
30+
}
31+
}
32+
});

types/tests/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "ES6",
44
"moduleResolution": "Node",
55
"alwaysStrict": true,
6+
"strict": true,
67
"noEmit": true
78
},
89
"include": [

types/utils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type DeepPartial<T> = T extends Function
1212
type _DeepPartialArray<T> = Array<DeepPartial<T>>
1313
type _DeepPartialObject<T> = { [P in keyof T]?: DeepPartial<T[P]> };
1414

15-
export type DistributiveArray<T> = T extends unknown ? T[] : never
15+
export type DistributiveArray<T> = [T] extends [unknown] ? Array<T> : never
1616

1717
// From https://stackoverflow.com/a/50375286
1818
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;

0 commit comments

Comments
 (0)