Skip to content

Commit 2e5b072

Browse files
authored
Turn on some TypeScript checks (chartjs#7116)
Turn on some TypeScript checks
1 parent ee7520b commit 2e5b072

11 files changed

+63
-51
lines changed

src/controllers/controller.line.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class LineController extends DatasetController {
2929
constructor(chart, datasetIndex) {
3030
super(chart, datasetIndex);
3131

32-
this._showLine = undefined;
32+
this._showLine = false;
3333
}
3434

3535
update(mode) {

src/core/core.controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ class Chart {
206206
this.active = undefined;
207207
this.lastActive = undefined;
208208
this._lastEvent = undefined;
209-
this._listeners = {resize: undefined};
209+
/** @type {{resize?: function}} */
210+
this._listeners = {};
210211
this._sortedMetasets = [];
211212
this._updating = false;
212213
this.scales = {};

src/core/core.scale.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {_factorize, toDegrees, toRadians} from '../helpers/helpers.math';
66
import {_parseFont, resolve, toPadding} from '../helpers/helpers.options';
77
import Ticks from './core.ticks';
88

9+
/**
10+
* @typedef { import("./core.controller").default } Chart
11+
*/
12+
913
defaults.set('scale', {
1014
display: true,
1115
offset: false,
@@ -227,10 +231,13 @@ class Scale extends Element {
227231

228232
/** @type {string} */
229233
this.id = cfg.id;
234+
/** @type {string} */
230235
this.type = cfg.type;
231236
/** @type {object} */
232237
this.options = cfg.options;
238+
/** @type {CanvasRenderingContext2D} */
233239
this.ctx = cfg.ctx;
240+
/** @type {Chart} */
234241
this.chart = cfg.chart;
235242

236243
// implements box
@@ -273,26 +280,24 @@ class Scale extends Element {
273280
this.min = undefined;
274281
this.max = undefined;
275282
/** @type {object[]} */
276-
this.ticks = null;
283+
this.ticks = [];
277284
/** @type {object[]|null} */
278285
this._gridLineItems = null;
279286
/** @type {object[]|null} */
280287
this._labelItems = null;
281288
/** @type {object|null} */
282289
this._labelSizes = null;
283-
/** @type {number} */
284-
this._length = undefined;
285-
/** @type {object} */
290+
this._length = 0;
286291
this._longestTextCache = {};
287292
/** @type {number} */
288293
this._startPixel = undefined;
289294
/** @type {number} */
290295
this._endPixel = undefined;
291-
this._reversePixels = undefined;
296+
this._reversePixels = false;
292297
this._userMax = undefined;
293298
this._userMin = undefined;
294-
this._ticksLength = undefined;
295-
this._borderValue = undefined;
299+
this._ticksLength = 0;
300+
this._borderValue = 0;
296301
}
297302

298303
/**
@@ -1418,7 +1423,9 @@ class Scale extends Element {
14181423
const position = options.position;
14191424
const isReverse = me.options.reverse;
14201425
let rotation = 0;
1421-
let scaleLabelX, scaleLabelY, textAlign;
1426+
/** @type CanvasTextAlign */
1427+
let textAlign;
1428+
let scaleLabelX, scaleLabelY;
14221429

14231430
if (me.isHorizontal()) {
14241431
switch (scaleLabelAlign) {

src/helpers/helpers.segment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ function solidSegments(points, start, max, loop) {
190190
if (!prev.skip) {
191191
loop = false;
192192
result.push({start: start % count, end: (end - 1) % count, loop});
193+
// @ts-ignore
193194
start = last = cur.stop ? end : null;
194195
}
195196
} else {

src/platform/platform.dom.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const EVENT_TYPES = {
4242
function readUsedSize(element, property) {
4343
const value = helpers.dom.getStyle(element, property);
4444
const matches = value && value.match(/^(\d+)(\.\d+)?px$/);
45-
return matches ? Number(matches[1]) : undefined;
45+
return matches ? +matches[1] : undefined;
4646
}
4747

4848
/**
@@ -110,19 +110,22 @@ function initCanvas(canvas, config) {
110110
* @private
111111
*/
112112
const supportsEventListenerOptions = (function() {
113-
let supports = false;
113+
let passiveSupported = false;
114114
try {
115-
const options = Object.defineProperty({}, 'passive', {
116-
// eslint-disable-next-line getter-return
117-
get() {
118-
supports = true;
115+
const options = {
116+
get passive() { // This function will be called when the browser attempts to access the passive property.
117+
passiveSupported = true;
118+
return false;
119119
}
120-
});
121-
window.addEventListener('e', null, options);
120+
};
121+
// @ts-ignore
122+
window.addEventListener('test', null, options);
123+
// @ts-ignore
124+
window.removeEventListener('test', null, options);
122125
} catch (e) {
123126
// continue regardless of error
124127
}
125-
return supports;
128+
return passiveSupported;
126129
}());
127130

128131
// Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.

src/scales/scale.category.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ class CategoryScale extends Scale {
88
constructor(cfg) {
99
super(cfg);
1010

11-
/** @type {number} */
12-
this._numLabels = undefined;
11+
this._numLabels = 0;
1312
/** @type {number} */
1413
this._startValue = undefined;
15-
/** @type {number} */
16-
this._valueRange = undefined;
14+
this._valueRange = 0;
1715
}
1816

1917
/**

src/scales/scale.linearbase.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ class LinearScaleBase extends Scale {
119119
this._startValue = undefined;
120120
/** @type {number} */
121121
this._endValue = undefined;
122-
/** @type {number} */
123-
this._valueRange = undefined;
122+
this._valueRange = 0;
124123
}
125124

126125
/**

src/scales/scale.logarithmic.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class LogarithmicScale extends Scale {
6868
this.end = undefined;
6969
/** @type {number} */
7070
this._startValue = undefined;
71-
/** @type {number} */
72-
this._valueRange = undefined;
71+
this._valueRange = 0;
7372
}
7473

7574
/**

src/scales/scale.radialLinear.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class RadialLinearScale extends LinearScaleBase {
302302
/** @type {number} */
303303
this.drawingArea = undefined;
304304
/** @type {string[]} */
305-
this.pointLabels = undefined;
305+
this.pointLabels = [];
306306
}
307307

308308
setDimensions() {

src/scales/scale.time.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@ import {_lookup, _lookupByKey} from '../helpers/helpers.collection';
77

88
/**
99
* @typedef { import("../core/core.adapters").Unit } Unit
10+
* @typedef {{common: boolean, size: number, steps?: number}} Interval
1011
*/
1112

1213
// Integer constants are from the ES6 spec.
1314
const MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
1415

1516
/**
16-
* @type {Map<Unit, {common: boolean, size: number, steps?: number}>}
17+
* @type {Object<Unit, Interval>}
1718
*/
18-
const INTERVALS = new Map();
19-
INTERVALS.set('millisecond', {common: true, size: 1, steps: 1000});
20-
INTERVALS.set('second', {common: true, size: 1000, steps: 60});
21-
INTERVALS.set('minute', {common: true, size: 60000, steps: 60});
22-
INTERVALS.set('hour', {common: true, size: 3600000, steps: 24});
23-
INTERVALS.set('day', {common: true, size: 86400000, steps: 30});
24-
INTERVALS.set('week', {common: false, size: 604800000, steps: 4});
25-
INTERVALS.set('month', {common: true, size: 2.628e9, steps: 12});
26-
INTERVALS.set('quarter', {common: false, size: 7.884e9, steps: 4});
27-
INTERVALS.set('year', {common: true, size: 3.154e10});
19+
const INTERVALS = {
20+
millisecond: {common: true, size: 1, steps: 1000},
21+
second: {common: true, size: 1000, steps: 60},
22+
minute: {common: true, size: 60000, steps: 60},
23+
hour: {common: true, size: 3600000, steps: 24},
24+
day: {common: true, size: 86400000, steps: 30},
25+
week: {common: false, size: 604800000, steps: 4},
26+
month: {common: true, size: 2.628e9, steps: 12},
27+
quarter: {common: false, size: 7.884e9, steps: 4},
28+
year: {common: true, size: 3.154e10}
29+
};
2830

2931
/**
3032
* @type {Unit[]}
3133
*/
32-
const UNITS = [];
33-
INTERVALS.forEach((v, k) => UNITS.push(k));
34+
const UNITS = /** @type Unit[] */(Object.keys(INTERVALS));
3435

3536
/**
3637
* @param {number} a
@@ -60,7 +61,7 @@ function arrayUnique(items) {
6061

6162
/**
6263
* @param {TimeScale} scale
63-
* {*} input
64+
* @param {*} input
6465
*/
6566
function parse(scale, input) {
6667
if (isNullOrUndef(input)) {
@@ -256,11 +257,10 @@ function interpolate(table, skey, sval, tkey) {
256257
*/
257258
function determineUnitForAutoTicks(minUnit, min, max, capacity) {
258259
const ilen = UNITS.length;
259-
let i, interval, factor;
260260

261-
for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
262-
interval = INTERVALS.get(UNITS[i]);
263-
factor = interval.steps ? interval.steps : MAX_INTEGER;
261+
for (let i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
262+
const interval = INTERVALS[UNITS[i]];
263+
const factor = interval.steps ? interval.steps : MAX_INTEGER;
264264

265265
if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
266266
return UNITS[i];
@@ -282,7 +282,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
282282
function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
283283
for (let i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
284284
const unit = UNITS[i];
285-
if (INTERVALS.get(unit).common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
285+
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
286286
return unit;
287287
}
288288
}
@@ -296,7 +296,7 @@ function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
296296
*/
297297
function determineMajorUnit(unit) {
298298
for (let i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) {
299-
if (INTERVALS.get(UNITS[i]).common) {
299+
if (INTERVALS[UNITS[i]].common) {
300300
return UNITS[i];
301301
}
302302
}
@@ -438,6 +438,7 @@ function setMajorTicks(scale, ticks, map, majorUnit) {
438438
*/
439439
function ticksFromTimestamps(scale, values, majorUnit) {
440440
const ticks = [];
441+
/** @type {Object<number,object>} */
441442
const map = {};
442443
const ilen = values.length;
443444
let i, value;
@@ -575,7 +576,7 @@ class TimeScale extends Scale {
575576
const time = options.time || (options.time = {});
576577
const adapter = this._adapter = new adapters._date(options.adapters.date);
577578

578-
579+
/** @type {{data: number[], labels: number[], all: number[]}} */
579580
this._cache = {
580581
data: [],
581582
labels: [],
@@ -584,7 +585,7 @@ class TimeScale extends Scale {
584585

585586
/** @type {Unit} */
586587
this._unit = 'day';
587-
/** @type {Unit | undefined} */
588+
/** @type {Unit=} */
588589
this._majorUnit = undefined;
589590
/** @type {object} */
590591
this._offsets = {};

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"allowSyntheticDefaultImports": true,
66
"allowJs": true,
77
"checkJs": true,
8-
"noEmit": true
8+
"noEmit": true,
9+
"alwaysStrict": true,
10+
"strictBindCallApply": true,
11+
"strictFunctionTypes": true
912
},
1013
"typedocOptions": {
1114
"name": "Chart.js",

0 commit comments

Comments
 (0)