8000 fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped con… · jakebailey/typescript-eslint@da02f61 · GitHub
[go: up one dir, main page]

Skip to content

Commit da02f61

Browse files
fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped constraint types if necessary (typescript-eslint#9740)
1 parent 478990f commit da02f61

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function collectTypeParameterUsageCounts(
303303
if (properties.length === 0) {
304304
// TS treats mapped types like `{[k in "a"]: T}` like `{a: T}`.
305305
// They have properties, so we need to avoid double-counting.
306-
visitType(type.templateType, false);
306+
visitType(type.templateType ?? type.constraintType, false);
307307
}
308308
}
309309

packages/eslint-plugin/tests/rules/no-unnecessary-type-parameters.test.ts

Lines changed: 27 additions & 0 deletions
D797
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,33 @@ ruleTester.run('no-unnecessary-type-parameters', rule, {
348348
fn: (key: K, val: V) => number,
349349
): number[];
350350
`,
351+
`
352+
declare function mappedReturnType<T extends string>(
353+
x: T,
354+
): { [K in T]: Capitalize<K> };
355+
356+
function inferredMappedReturnType<T extends string>(x: T) {
357+
return mappedReturnType(x);
358+
}
359+
`,
360+
`
361+
declare function mappedReturnType<T extends string>(
362+
x: T,
363+
): { [K in T]: Capitalize<K> };
364+
365+
function inferredMappedReturnType<T extends string>(x: T) {
366+
return () => mappedReturnType(x);
367+
}
368+
`,
369+
`
370+
declare function mappedReturnType<T extends string>(
371+
x: T,
372+
): { [K in T]: Capitalize<K> };
373+
374+
function inferredMappedReturnType<T extends string>(x: T) {
375+
return [{ value: () => mappedReturnType(x) }];
376+
}
377+
`,
351378
],
352379

353380
invalid: [

0 commit comments

Comments
 (0)
0