Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prefer-destructuring] Disable preferring Array destructuring to not clash with no-unreadable-array-destructuring #209

Open
ThaNarie opened this issue Jan 25, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ThaNarie
Copy link
Member

We have enabled https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-unreadable-array-destructuring.md

To disallow things like const [,,,, foo] = parts;

Which prefers const foo = parts[3].

But this clashes with https://eslint.org/docs/latest/rules/prefer-destructuring

As no-unreadable-array-destructuring says at the bottom of their page, it's advised to disable the preference of array destructuring.

It would be nice if we could only force destructuring for array[0] and array[1], but maybe that will be an option in the future.

Overview of what we like, especially for tuples.

const t: [string, number, boolean] = ['hello', 42, true];

const s1 = t[0]; // unfortunately allowed
const s2 = t.at(0);
const [s3] = t;// always preferred

const n1 = t[1]; // unfortunately allowed
const n2 = t.at(1);
const [,n3] = t; // always preferred

const b1 = t[2]; // preferred for tuples, since it keeps type information of the tuple slots
const b2 = t.at(2); // preferred for arrays (especially when using negative indexes, and works better for typing when length is unknown)
const [,,b3] = t; // lint error
@ThaNarie ThaNarie added the bug Something isn't working label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant