Optional argument can appear before non-optional parameters
See original GitHub issueThis code produces 2 warnings:
function f(a=1,b) {}
f(undefined, 3);
JSC_OPTIONAL_ARG_AT_END: optional arguments must be at the end at line 1 character 0
function f(a=1,b) {}
^
JSC_WRONG_ARGUMENT_COUNT: Function f: called with 2 argument(s). Function requires at least 0 argument(s) and no more than 1 argument(s). at line 2 character 0
f(undefined, 3);
^
And it cannot be suppressed with @suppress {functionParams}
.
It is legal ES6 for a parameter with a default value to be followed by a required one, it should not be considered optional.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Named and Optional Arguments - C# Programming Guide
Named arguments in C# specify arguments by name, not position. Optional arguments can be omitted.
Read more >Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default values. You'll also learn how to create...
Read more >Behaviour when declaring non-optional parameter after ...
The presence or absence of default values for a parameter does not in any way influence what arguments will be assigned to what...
Read more >C# Optional Parameters: Everything You Need to Know
If we do not pass optional argument value at calling time, it uses the default value. Inside the method definition, we define a...
Read more >"Optional" should not be used for parameters
... you have to do in the method without really increasing the value. With an Optional parameter, you go from having 2 possible...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In this case, state is a required parameter whose type includes undefined. That’s fine.
When the compiler sees an unannotated function such as
function f(a=1,b) {}
, it needs to decide whethera
is required or optional. I think that in most cases the user wants it to be optional, so we would give spurious warnings when it was meant to be required. If we considered it to be required, there would be more spurious warnings when f is called without any arguments. It’s a tradeoff. If the user wants something that is not the default compiler assumption, they can just manually annotate their code.But I do agree that the current situation of early warning is somewhat unsatisfactory, and we should just consider the trailing parameters optional and not warn.
Right.