How to check the wrong number of parameters passed to a function

I am writing a program using typescript and tslint as linter. My current list of rules is the following (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 } } 

Although I use Tslint, it cannot catch a function call with the wrong number of parameters. For example, I have the following function:

 let displayTimer: Function = function(): void { document.getElementById('milliseconds').innerHTML = ms.toString(); document.getElementById('seconds').innerHTML = seconds.toString(); document.getElementById('minutes').innerHTML= minutes.toString(); }; 

And I call this from inside another function like this:

 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); }; 

As you can see, I am passing the parameter to the displayTimer function (in this case, number 1, but it could be something else) and linter will not catch this.

+8
javascript typescript tslint
source share
1 answer

Just remove the Function type and TypeScript will verify the signature:

 let displayTimer = function(): void { // ... }; displayTimer(1); // Error: Supplied parameters does not match any signature of call target 

The derived displayTimer type displayTimer not Function (which accepts any signatures), but () => void .

See the code in PlayGround .

+5
source share

All Articles