Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 927 Bytes

no-object-as-default-parameter.md

File metadata and controls

39 lines (28 loc) · 927 Bytes

Disallow the use of objects as default parameters

This rule is part of the recommended config.

Default parameters should not be passed to a function through an object literal. The foo = {a: false} parameter works fine if only used with one option. As soon as additional options are added, you risk replacing the whole foo = {a: false, b: true} object when passing only one option: {a: true}. For this reason, object destructuring should be used instead.

Fail

const abc = (foo = {a: false}) => {};
function foo({a} = {a: false}) {}
const abc = (foo = {a: false, b: 123}) => {};

Pass

const abc = (foo = {}) => {};
function foo(options) {
	const {a} = {a: false, ...options};
}
const abc = (foo = false) => {};
const foo = ({a = false, b = 123}) => {};