Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
MemberAuthor

@ronamironamiDec 13, 2024
edited
Loading

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 ofan edge case with TypeScript's assignability.

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.

ronami reacted with thumbs up emoji
`,
// '{}' 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
MemberAuthor

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.

JoshuaKGoldberg reacted with thumbs up emoji
`,
`
function f<T>() {
['a', 'b'].reduce(
(accum, name) => ({
...accum,
[name]: true,
}),
{} as T,
);
}
`,
`
['a', 'b'].reduce((accum, name) => \`\${accum} | hello \${name}!\`);
Copy link
MemberAuthor

@ronamironamiDec 13, 2024
edited
Loading

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.

JoshuaKGoldberg reacted with thumbs up emoji
`,
],
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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp