我正在写一个程序,使用typescript和tslint作为linter。我目前最喜欢的规则列表如下(tslint.json):
{
"extends": "tslint:recommended",
"rules": {
"comment-format": [false, "check-space"],
"eofline": false,
"triple-equals": [false, "allow-null-check"],
"no-trailing-whitespace": false,
"one-line": false,
"no-empty": false,
"typedef-whitespace": false,
"whitespace": false,
"radix": false,
"no-consecutive-blank-lines": false,
"no-console": false,
"typedef": [true,
"variable-declaration",
"call-signature",
"parameter",
"property-declaration",
"member-variable-declaration"
],
"quotemark": false,
"no-any": true,
"one-variable-per-declaration": false
}
}尽管我使用的是Tslint,但它无法捕获对参数数量错误的函数的调用。例如,我有以下函数:
let displayTimer: Function = function(): void {
document.getElementById('milliseconds').innerHTML = ms.toString();
document.getElementById('seconds').innerHTML = seconds.toString();
document.getElementById('minutes').innerHTML= minutes.toString();
};我从另一个函数内部调用它,如下所示:
let turnTimerOn: Function = function(): void {
ms += interval;
if (ms >= 1000)
{
ms = 0;
seconds += 1;
}
if (seconds >= 60)
{
ms = 0;
seconds = 0;
minutes += 1;
}
displayTimer(1);
};正如您所看到的,我向displayTimer函数传递了一个参数(在本例中是数字1,但也可以是其他任何值),而linter没有捕捉到这个参数。
发布于 2016-07-22 21:43:26
只需移除类型Function,TypeScript就会检查签名:
let displayTimer = function(): void {
// ...
};
displayTimer(1); // Error: Supplied parameters does not match any signature of call target推断出的displayTimer类型不是Function (它接受任何签名),而是() => void。
参见the code in the PlayGround。
https://stackoverflow.com/questions/38527011
复制相似问题