-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New Rule: prefer-destructuring
#1931
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
Comments
could you please give me a self-contained example? |
Does this work? import { Request, Response, NextFunction } from 'express'
async function example(
req: Request,
_res: Response,
_next: NextFunction,
): Promise<void> {
// Type 'ParamsDictionary' must have a '[Symbol.iterator]()' method that returns an iterator
const [param] = req.params
console.log(param)
} |
what was the code before you fixed it? |
Yes, which the base ESLint |
Thanks. |
The correct way to destructure this is const { 0: param } = req.params because |
Another issue with the eslint const obj = {xs: ['a', 'b', 'c']}; // type is {xs: string[]}
const xs: readonly string[] = obj.xs;
xs.push('d');
// ~~~~ Property 'push' does not exist on type 'readonly string[]'. Line two gets flagged by eslint's rule: However, applying the quick fix removes the type annotation, an unsafe operation which makes the const obj = {xs: ['a', 'b', 'c']}; // type is {xs: string[]}
const {xs} = obj;
xs.push('d'); // ok! oh no! A typescript-eslint |
Isn't it a duplication of #723? This one had a lot more interaction than the former though. |
No - they both want to extend the same extension rule - but they want different things. THIS issue is about #723 is about |
Oh yes, I noticed that too, but as (#1931 (comment)) was talking about the same of #723 (and received so many upvotes), I thought all related things were being tracked here. |
- DIsable `prefer-destructuring` in TypeScript, as it is potentially desctructive typescript-eslint/typescript-eslint#1931 - Set any rules that could conflict with Prettier to match @jessety/prettier-config - Disable `@typescript-eslint/no-inferrable-types` - I've found it to be unhelpful - Fix issue where JS tests were treated like TS tests New rules - `lines-between-class-members` - Set `import/no-default-export` to `warn`
Another issue with core eslint-rule: export const construct: Function = Reflect.construct;
// Fixed to:
export const { construct } = Reflect; As you can see, core rule auto-fix removes type definition (": Function"). |
Hi, I'm working on this issue. const a = {0: 1};
const b = a[0]; // this is incorrect iff options[0].object is true and options[1].enforceForRenamedProperties is true
const {0: c} = a; // this is always correct
const x = [1];
const y = x[0]; // works same as the current ESLint rule
const v: any = {0: 1};
const w = x[0]; // works same as the current ESLint rule Showing other edge cases are welcome. |
I'm also working on #1931 (comment), #1931 (comment) and #723. |
Sorry, I don't follow what you're asking about @seiyab. There are no rule reports from the base |
@JoshuaKGoldberg const a = {0: 1};
const b = a[0]; // this is incorrect iff options[0].object is true and options[1].enforceForRenamedProperties is true In this case, the proper destructuring syntax is
Related document I have written: https://github.com/typescript-eslint/typescript-eslint/pull/7117/files#diff-0d683d5803f89de0a159ca7ef7461802afac69ae7fbf459411553aa36e5a7cbaR49-R50 And I asked that whether my thoughts make sense or not. Does it get clear? |
"this will be incorrect iff ..." might be better than "this is incorrect iff ...". |
Can this issue be closed? ref PR #7117 |
I'd like to use the
prefer-destructuring
rule from ESLint, however, when I try to use array destructuring with an Express request object, I get the following TypeScript Error:Type 'ParamsDictionary' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
.That seems reasonable, but then the base ESLint
prefer-destructuring
rule gives me a warning, even though TypeScript claims I'm not allowed to use array destructuring here.I think having a type-aware version of this rule would be beneficial.
The text was updated successfully, but these errors were encountered: