Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am writing a program using typescript and tslint as a linter. My current favorite 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 am using Tslint it fails to catch a calling to a function with 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 am calling it with 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 a parameter to the displayTimer function (in this case the number 1 but it could be anything else) and the linter is not catching that.

like image 834
skiabox Avatar asked Jul 22 '16 12:07

skiabox


People also ask

How do I find out the number of parameters passed into a function?

To get the number of arguments that were passed into your function, call func_num_args() and read its return value. To get the value of an individual parameter, use func_get_arg() and pass in the parameter number you want to retrieve to have its value returned back to you.

How many parameters arguments can be passed to a function?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

What is wrong argument number?

The number of arguments to a procedure must match the number of parameters in the procedure's definition. This error has the following causes and solutions: The number of arguments in the call to the procedure wasn't the same as the number of required arguments expected by the procedure.

How many parameters are passed in replace ()?

Replace method is the string class method of java, which takes two parameters.


1 Answers

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

let displayTimer = function(): void {
    // ...
};

displayTimer(1); // Error: Supplied parameters does not match any signature of call target

The inferred type of displayTimer is not Function (which accepts any signatures) but () => void.

See the code in the PlayGround.

like image 76
Paleo Avatar answered Oct 07 '22 00:10

Paleo