Default values should be of expected types

  • MISMATCHED_TYPE_OF_DEFAULT_VALUE
  • Error
  • Medium
  • No tags

This rule applies when a default value is not of the expected type and fails to provide methods of the expected type.

When a non-existent method is called as a result of mismatched default values, a TypeError exception occurs. This includes the case that a non-iterable object like an empty object literal is used where an iterable object is required such as for-of loop.

Currently, this rule applies to the case where the expected type is an array and the wrong default value is an empty object literal, string, boolean, or number.

Note: This rule may report false alarms if the Object.prototype is extended with methods having the same name as array methods. We recommend disabling this rule in such cases.

Noncompliant Code Example

View with compliant examples side by side
function example(arr) {
    arr = arr || {}; // MISMATCHED_TYPE_OF_DEFAULT_VALUE alarm because 'arr' is an array expectedly.
    arr.forEach(foo);
}

Compliant Code Example

View with noncompliant examples side by side
function example(arr) {
    arr = arr || [];
    arr.forEach(foo);
}

Version

This rule was introduced in DeepScan 1.33.0.