8000 New rule: use type parameter for .reduce instead of a cast · Issue #1604 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

New rule: use type parameter for .reduce instead of a cast 8000 #1604

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

Closed
JoshuaKGoldberg opened this issue Feb 13, 2020 · 1 comment · Fixed by #1707
Closed

New rule: use type parameter for .reduce instead of a cast #1604

JoshuaKGoldberg opened this issue Feb 13, 2020 · 1 comment · Fixed by #1707
Labels
enhancement: new plugin rule New rule request for eslint-plugin has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member
JoshuaKGoldberg commented Feb 13, 2020

Repro

const names = ['a', 'b'];

const record = names.reduce(
  (accum, name) => ({
    ...accum,
    [name]: true,
  }),
  {} as Record<string, boolean>
);

Expected Result

Typo! It should be Record<string, boolean>, but because we're using a cast, it's missed. Ah, this does fire a TS error, thanks - updated the sample.

Also, u Using a cast is unnecessary because Array#reduce can take in a type parameter indicating the default value & return type. The above code would be better written as:

const names = ['a', 'b'];

const record = names.reduce<Record<string, boolean>>(
  (accum, name) => ({
    ...accum,
    [name]: true,
  }),
  {} 
);

Actual Result

No error.

Versions

package version
@typescript-eslint/eslint-plugin 2.7.0
@typescript-eslint/parser 2.7.0
TypeScript 3.7.3
ESLint 5.15.1
node 12.4.0
npm 6.9.0
@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 13, 2020
@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look labels Feb 13, 2020
@bradzacher
Copy link
Member

Your example fires a TS error, so that does get caught.
image

Though I agree - I much prefer using the type parameter. Sounds like a good stylistic rule.

@bradzacher bradzacher added the has pr there is a PR raised to close this label Mar 8, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0