Skip to content

fix(eslint-plugin): [prefer-reduce-type-parameter] don't report cases in which the fix results in a type error #10494

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-reduce-type-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,29 @@ export default createRule({

const [, secondArg] = callee.parent.arguments;

if (callee.parent.arguments.length < 2 || !isTypeAssertion(secondArg)) {
if (callee.parent.arguments.length < 2) {
return;
}

if (isTypeAssertion(secondArg)) {
const initializerType = services.getTypeAtLocation(
secondArg.expression,
);

const assertedType = services.getTypeAtLocation(
secondArg.typeAnnotation,
);

const isAssertionNecessary = !checker.isTypeAssignableTo(
initializerType,
assertedType,
);

// don't report this if the resulting fix will be a type error
if (isAssertionNecessary) {
return;
}
} else {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,70 @@ ruleTester.run('prefer-reduce-type-parameter', rule, {
return a.concat(1);
}, [] as number[]);
`,
`
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{} as Record<'a' | 'b', boolean>,
);
`,
// Object literal may only specify known properties, and 'c' does not exist in
// type 'Record<"a" | "b", boolean>'.
`
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{ a: true, b: false, c: true } as Record<'a' | 'b', boolean>,
);
Comment on lines +72 to +78
Copy link
Member Author

@ronami ronami Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's necessary to cover this, but it's somewhat of an edge case with TypeScript's assignability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, yeah, I say let's cover it.

`,
// '{}' is assignable to the constraint of type 'T', but 'T' could be
// instantiated with a different subtype of constraint 'Record<string, boolean>'.
`
function f<T extends Record<string, boolean>>() {
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{} as T,
);
}
Comment on lines +83 to +91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a missing edge case in #10453.

`,
`
function f<T>() {
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{} as T,
);
}
`,
`
['a', 'b'].reduce((accum, name) => \`\${accum} | hello \${name}!\`);
Copy link
Member Author

@ronami ronami Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't directly related to the change, but it was previously missing coverage, and because it is on its own line now, it fails codecov.

`,
],
invalid: [
{
code: `
declare const arr: string[];
arr.reduce<string>(acc => acc, arr.shift() as string);
arr.reduce<string | undefined>(acc => acc, arr.shift() as string | undefined);
`,
errors: [
{
column: 32,
column: 44,
line: 3,
messageId: 'preferTypeParameter',
},
],
output: `
declare const arr: string[];
arr.reduce<string>(acc => acc, arr.shift());
arr.reduce<string | undefined>(acc => acc, arr.shift());
`,
},
{
Expand Down Expand Up @@ -275,5 +322,63 @@ declare const tuple: [number, number, number] & number[];
tuple.reduce<number[]>((a, s) => a.concat(s * 2), []);
`,
},
{
code: `
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{} as Record<string, boolean>,
);
`,
errors: [
{
column: 3,
line: 7,
messageId: 'preferTypeParameter',
},
],
output: `
['a', 'b'].reduce<Record<string, boolean>>(
(accum, name) => ({
...accum,
[name]: true,
}),
{},
);
`,
},
{
code: `
function f<T extends Record<string, boolean>>(t: T) {
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
t as Record<string, boolean | number>,
);
}
`,
errors: [
{
column: 5,
line: 8,
messageId: 'preferTypeParameter',
},
],
output: `
function f<T extends Record<string, boolean>>(t: T) {
['a', 'b'].reduce<Record<string, boolean | number>>(
(accum, name) => ({
...accum,
[name]: true,
}),
t,
);
}
`,
},
],
});
Loading