Destructuring is a convenient way of extracting multiple values from data stored in (possibly nested) objects and arrays. It can make code more concise and expressive by directly extracting values or properties needed from arrays or objects. However, it is possible to define an empty pattern that has no effect, where no variables are bound to the destructured values.
let {a: {}} = myObj; // Noncompliant: this does not create any variable
function foo({p: []}) { // Noncompliant: this does not define any parameter
// ...
}
When empty curly or square brackets are bound to a pattern with a colon (:), like { pattern: [] } or { pattern: {}
}, the intent is likely to define a default value. To properly define such a default value, use the assignment operator (=)
instead.
let {a = {}} = myObj;
function foo({p = []}) {
// ...
}
If that is not the intention, complete the destructuring pattern to contain the variables to create.
let {a: {b, c}} = myObj;
function foo({p: [a, b, c]}) {
// ...
}
Empty object patterns are intentional in TypeScript-specific function contexts such as interface method signatures, function type aliases, ambient declarations, and abstract class methods:
interface EmptyProps {}
// Compliant: interface method signature
interface Component {
render({}: EmptyProps): void;
}
// Compliant: function type alias
type NoPropsComponent = ({}: EmptyProps) => void;
// Compliant: ambient function declaration
declare function useHook({}: EmptyProps): void;
// Compliant: abstract class method
abstract class AbstractComponent {
abstract render({}: EmptyProps): void;
}