Lint Rules

useDefaultParameterLast (since v11.0.0)

Enforce default function parameters and optional parameters to be last.

Default and optional parameters that precede a required parameter cannot be omitted at call site.

Examples

Invalid

function f(a = 0, b) {}
nursery/useDefaultParameterLast.js:1:12 lint/nursery/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━

   The default parameter should follow the required parameter or should be a required parameter.
  
  > 1 │ function f(a = 0, b) {}
              ^^^^^
    2 │ 
  
   The required parameter is here:
  
  > 1 │ function f(a = 0, b) {}
                     ^
    2 │ 
  
   A default parameter that precedes a required parameter cannot be omitted at call site.
  
   Suggested fix: Turn the parameter into a required parameter.
  
    1 │ function·f(a·=·0,·b)·{}
               ---       
function f(a, b = 0, c) {}
nursery/useDefaultParameterLast.js:1:15 lint/nursery/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━

   The default parameter should follow the required parameter or should be a required parameter.
  
  > 1 │ function f(a, b = 0, c) {}
                 ^^^^^
    2 │ 
  
   The required parameter is here:
  
  > 1 │ function f(a, b = 0, c) {}
                        ^
    2 │ 
  
   A default parameter that precedes a required parameter cannot be omitted at call site.
  
   Suggested fix: Turn the parameter into a required parameter.
  
    1 │ function·f(a,·b·=·0,·c)·{}
                  ---       
function f(a: number, b?: number, c: number) {}
nursery/useDefaultParameterLast.js:1:23 lint/nursery/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━

   The optional parameter should follow the required parameter or should be a required parameter.
  
  > 1 │ function f(a: number, b?: number, c: number) {}
                         ^^^^^^^^^^
    2 │ 
  
   The required parameter is here:
  
  > 1 │ function f(a: number, b?: number, c: number) {}
                                     ^^^^^^^^^
    2 │ 
  
   A optional parameter that precedes a required parameter cannot be omitted at call site.
  
   Suggested fix: Turn the parameter into a required parameter.
  
    1 │ function·f(a:·number,·b?:·number,·c:·number)·{}
                         -                       
class Foo {
    constructor(readonly a = 10, readonly b: number) {}
}
nursery/useDefaultParameterLast.js:2:17 lint/nursery/useDefaultParameterLast  FIXABLE  ━━━━━━━━━━━━━

   The default parameter should follow the required parameter or should be a required parameter.
  
    1 │ class Foo {
  > 2 │     constructor(readonly a = 10, readonly b: number) {}
                   ^^^^^^^^^^^^^^^
    3 │ }
    4 │ 
  
   The required parameter is here:
  
    1 │ class Foo {
  > 2 │     constructor(readonly a = 10, readonly b: number) {}
                                    ^^^^^^^^^^^^^^^^^^
    3 │ }
    4 │ 
  
   A default parameter that precedes a required parameter cannot be omitted at call site.
  
   Suggested fix: Turn the parameter into a required parameter.
  
    2 │ ····constructor(readonly·a·=·10,·readonly·b:·number)·{}
                             ----                        

Valid

function f(a, b = 0) {}
function f(a: number, b?: number, c = 0) {}
function f(a: number, b = 0, c?: number) {}