Turn on some TypeScript checks (#7116)

Turn on some TypeScript checks
This commit is contained in:
Ben McCann 2020-02-20 06:43:16 -08:00 committed by GitHub
parent ee7520b710
commit 2e5b072251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 63 additions and 51 deletions

View File

@ -29,7 +29,7 @@ class LineController extends DatasetController {
constructor(chart, datasetIndex) {
super(chart, datasetIndex);
this._showLine = undefined;
this._showLine = false;
}
update(mode) {

View File

@ -206,7 +206,8 @@ class Chart {
this.active = undefined;
this.lastActive = undefined;
this._lastEvent = undefined;
this._listeners = {resize: undefined};
/** @type {{resize?: function}} */
this._listeners = {};
this._sortedMetasets = [];
this._updating = false;
this.scales = {};

View File

@ -6,6 +6,10 @@ import {_factorize, toDegrees, toRadians} from '../helpers/helpers.math';
import {_parseFont, resolve, toPadding} from '../helpers/helpers.options';
import Ticks from './core.ticks';
/**
* @typedef { import("./core.controller").default } Chart
*/
defaults.set('scale', {
display: true,
offset: false,
@ -227,10 +231,13 @@ class Scale extends Element {
/** @type {string} */
this.id = cfg.id;
/** @type {string} */
this.type = cfg.type;
/** @type {object} */
this.options = cfg.options;
/** @type {CanvasRenderingContext2D} */
this.ctx = cfg.ctx;
/** @type {Chart} */
this.chart = cfg.chart;
// implements box
@ -273,26 +280,24 @@ class Scale extends Element {
this.min = undefined;
this.max = undefined;
/** @type {object[]} */
this.ticks = null;
this.ticks = [];
/** @type {object[]|null} */
this._gridLineItems = null;
/** @type {object[]|null} */
this._labelItems = null;
/** @type {object|null} */
this._labelSizes = null;
/** @type {number} */
this._length = undefined;
/** @type {object} */
this._length = 0;
this._longestTextCache = {};
/** @type {number} */
this._startPixel = undefined;
/** @type {number} */
this._endPixel = undefined;
this._reversePixels = undefined;
this._reversePixels = false;
this._userMax = undefined;
this._userMin = undefined;
this._ticksLength = undefined;
this._borderValue = undefined;
this._ticksLength = 0;
this._borderValue = 0;
}
/**
@ -1418,7 +1423,9 @@ class Scale extends Element {
const position = options.position;
const isReverse = me.options.reverse;
let rotation = 0;
let scaleLabelX, scaleLabelY, textAlign;
/** @type CanvasTextAlign */
let textAlign;
let scaleLabelX, scaleLabelY;
if (me.isHorizontal()) {
switch (scaleLabelAlign) {

View File

@ -190,6 +190,7 @@ function solidSegments(points, start, max, loop) {
if (!prev.skip) {
loop = false;
result.push({start: start % count, end: (end - 1) % count, loop});
// @ts-ignore
start = last = cur.stop ? end : null;
}
} else {

View File

@ -42,7 +42,7 @@ const EVENT_TYPES = {
function readUsedSize(element, property) {
const value = helpers.dom.getStyle(element, property);
const matches = value && value.match(/^(\d+)(\.\d+)?px$/);
return matches ? Number(matches[1]) : undefined;
return matches ? +matches[1] : undefined;
}
/**
@ -110,19 +110,22 @@ function initCanvas(canvas, config) {
* @private
*/
const supportsEventListenerOptions = (function() {
let supports = false;
let passiveSupported = false;
try {
const options = Object.defineProperty({}, 'passive', {
// eslint-disable-next-line getter-return
get() {
supports = true;
const options = {
get passive() { // This function will be called when the browser attempts to access the passive property.
passiveSupported = true;
return false;
}
});
window.addEventListener('e', null, options);
};
// @ts-ignore
window.addEventListener('test', null, options);
// @ts-ignore
window.removeEventListener('test', null, options);
} catch (e) {
// continue regardless of error
}
return supports;
return passiveSupported;
}());
// Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.

View File

@ -8,12 +8,10 @@ class CategoryScale extends Scale {
constructor(cfg) {
super(cfg);
/** @type {number} */
this._numLabels = undefined;
this._numLabels = 0;
/** @type {number} */
this._startValue = undefined;
/** @type {number} */
this._valueRange = undefined;
this._valueRange = 0;
}
/**

View File

@ -119,8 +119,7 @@ class LinearScaleBase extends Scale {
this._startValue = undefined;
/** @type {number} */
this._endValue = undefined;
/** @type {number} */
this._valueRange = undefined;
this._valueRange = 0;
}
/**

View File

@ -68,8 +68,7 @@ class LogarithmicScale extends Scale {
this.end = undefined;
/** @type {number} */
this._startValue = undefined;
/** @type {number} */
this._valueRange = undefined;
this._valueRange = 0;
}
/**

View File

@ -302,7 +302,7 @@ class RadialLinearScale extends LinearScaleBase {
/** @type {number} */
this.drawingArea = undefined;
/** @type {string[]} */
this.pointLabels = undefined;
this.pointLabels = [];
}
setDimensions() {

View File

@ -7,30 +7,31 @@ import {_lookup, _lookupByKey} from '../helpers/helpers.collection';
/**
* @typedef { import("../core/core.adapters").Unit } Unit
* @typedef {{common: boolean, size: number, steps?: number}} Interval
*/
// Integer constants are from the ES6 spec.
const MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
/**
* @type {Map<Unit, {common: boolean, size: number, steps?: number}>}
* @type {Object<Unit, Interval>}
*/
const INTERVALS = new Map();
INTERVALS.set('millisecond', {common: true, size: 1, steps: 1000});
INTERVALS.set('second', {common: true, size: 1000, steps: 60});
INTERVALS.set('minute', {common: true, size: 60000, steps: 60});
INTERVALS.set('hour', {common: true, size: 3600000, steps: 24});
INTERVALS.set('day', {common: true, size: 86400000, steps: 30});
INTERVALS.set('week', {common: false, size: 604800000, steps: 4});
INTERVALS.set('month', {common: true, size: 2.628e9, steps: 12});
INTERVALS.set('quarter', {common: false, size: 7.884e9, steps: 4});
INTERVALS.set('year', {common: true, size: 3.154e10});
const INTERVALS = {
millisecond: {common: true, size: 1, steps: 1000},
second: {common: true, size: 1000, steps: 60},
minute: {common: true, size: 60000, steps: 60},
hour: {common: true, size: 3600000, steps: 24},
day: {common: true, size: 86400000, steps: 30},
week: {common: false, size: 604800000, steps: 4},
month: {common: true, size: 2.628e9, steps: 12},
quarter: {common: false, size: 7.884e9, steps: 4},
year: {common: true, size: 3.154e10}
};
/**
* @type {Unit[]}
*/
const UNITS = [];
INTERVALS.forEach((v, k) => UNITS.push(k));
const UNITS = /** @type Unit[] */(Object.keys(INTERVALS));
/**
* @param {number} a
@ -60,7 +61,7 @@ function arrayUnique(items) {
/**
* @param {TimeScale} scale
* {*} input
* @param {*} input
*/
function parse(scale, input) {
if (isNullOrUndef(input)) {
@ -256,11 +257,10 @@ function interpolate(table, skey, sval, tkey) {
*/
function determineUnitForAutoTicks(minUnit, min, max, capacity) {
const ilen = UNITS.length;
let i, interval, factor;
for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
interval = INTERVALS.get(UNITS[i]);
factor = interval.steps ? interval.steps : MAX_INTEGER;
for (let i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
const interval = INTERVALS[UNITS[i]];
const factor = interval.steps ? interval.steps : MAX_INTEGER;
if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
return UNITS[i];
@ -282,7 +282,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
for (let i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
const unit = UNITS[i];
if (INTERVALS.get(unit).common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
return unit;
}
}
@ -296,7 +296,7 @@ function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
*/
function determineMajorUnit(unit) {
for (let i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) {
if (INTERVALS.get(UNITS[i]).common) {
if (INTERVALS[UNITS[i]].common) {
return UNITS[i];
}
}
@ -438,6 +438,7 @@ function setMajorTicks(scale, ticks, map, majorUnit) {
*/
function ticksFromTimestamps(scale, values, majorUnit) {
const ticks = [];
/** @type {Object<number,object>} */
const map = {};
const ilen = values.length;
let i, value;
@ -575,7 +576,7 @@ class TimeScale extends Scale {
const time = options.time || (options.time = {});
const adapter = this._adapter = new adapters._date(options.adapters.date);
/** @type {{data: number[], labels: number[], all: number[]}} */
this._cache = {
data: [],
labels: [],
@ -584,7 +585,7 @@ class TimeScale extends Scale {
/** @type {Unit} */
this._unit = 'day';
/** @type {Unit | undefined} */
/** @type {Unit=} */
this._majorUnit = undefined;
/** @type {object} */
this._offsets = {};

View File

@ -5,7 +5,10 @@
"allowSyntheticDefaultImports": true,
"allowJs": true,
"checkJs": true,
"noEmit": true
"noEmit": true,
"alwaysStrict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true
},
"typedocOptions": {
"name": "Chart.js",