Skip to content

Bug: [prefer-reduce-type-parameter] Quick-fix mis-handles null-prototype objects #7510

Closed as not planned
@nickshanks

Description

@nickshanks

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.2.1-rc&fileType=.tsx&code=NoXQdATgpgJgrgYygCgB4AIC8A%2BdqA06A3ugPqkAOEA9gC7XkBc6AdnADbvoC%2B6AhgGd0cFgGsW1AO4t%2BQgEpQE1CDAA8A2hACWLAOaEN2vdgCUAbgBQQA&eslintrc=N4KABGBEAOCGBOBnApvSAuKABALgT2mUQGN4BLaHAWiIBsyA7HAejiVUgBpwo2V4A8pTIB7BogxhQECDHgiAVsmI5JOeAFdk3GVBwkxAMzIBzAEoiROACJk0mSAH1HAEzsNYAW2SQeAXx0oTVoiSWkZSFwCIlIKajpGFmh4ZENUKhSXDWJkKnxCKjYvZBwOB1R5NH8QPyA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

[].reduce(x=>x, { __proto__: null } as unknown as Record<string, string>);

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/prefer-reduce-type-parameter": ["error"],
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

Quick Fix would transform the code to:

[].reduce<Record<string, string>>(x=>x, Object.create(null));

Actual Result

After applying Quick Fix:

[].reduce<Record<string, string>>(x=>x, { __proto__: null } as unknown);
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Argument of type 'unknown' is not assignable to parameter of type 'Record<string, string>'. ts(2345)"

Then after second Quick Fix:

[].reduce<Record<string, string>>(x=>x, { __proto__: null });
                                          ~~~~~~~~~

"Type 'null' is not assignable to type 'string'. ts(2322)"

Additional Info

Object.create(null) does not have the TypeScript problems that { __proto__: null } does, and the latter should be transformed into the former before the pair of Quick Fixes are applied. All three transformations are required for eslint --fix to not break anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingduplicateThis issue or pull request already existspackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions