-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, | ||
); | ||
`, | ||
// '{}' 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}!\`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
`, | ||
}, | ||
{ | ||
|
@@ -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, | ||
); | ||
} | ||
`, | ||
}, | ||
], | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.