Skip to content

Commit 94f7c99

Browse files
fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped alias type arguments (typescript-eslint#9741)
* fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped alias type arguments * yarn format --write * Handle type aliases higher up in typescript-eslint#9741 (typescript-eslint#323) handle type aliases higher up --------- Co-authored-by: Dan Vanderkam <danvdk@gmail.com>
1 parent 0c5382e commit 94f7c99

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ function collectTypeParameterUsageCounts(
258258
}
259259
}
260260

261+
// Catch-all: generic type references like `Exclude<T, null>`
262+
else if (type.aliasTypeArguments) {
263+
// We don't descend into the definition of the type alias, so we don't
264+
// know whether it's used multiple times. It's safest to assume it is.
265+
visitTypesList(type.aliasTypeArguments, true);
266+
}
267+
261268
// Intersections and unions like `0 | 1`
262269
else if (tsutils.isUnionOrIntersectionType(type)) {
263270
visitTypesList(type.types, assumeMultipleUses);
@@ -307,10 +314,6 @@ function collectTypeParameterUsageCounts(
307314
}
308315
}
309316

310-
for (const typeArgument of type.aliasTypeArguments ?? []) {
311-
visitType(typeArgument, true);
312-
}
313-
314317
visitType(type.getNumberIndexType(), true);
315318
visitType(type.getStringIndexType(), true);
316319

@@ -329,11 +332,6 @@ function collectTypeParameterUsageCounts(
329332
else if (isOperatorType(type)) {
330333
visitType(type.type, assumeMultipleUses);
331334
}
332-
333-
// Catch-all: generic type references like `Exclude<T, null>`
334-
else if (type.aliasTypeArguments) {
335-
visitTypesList(type.aliasTypeArguments, true);
336-
}
337335
}
338336

339337
function incrementIdentifierCount(

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ ruleTester.run('no-unnecessary-type-parameters', rule, {
342342
(token): token is Exclude<TSESTree.Token, Conditions & ExtractedToken> =>
343343
tokenType in conditions;
344344
`,
345+
`
346+
type Foo<T, S> = S extends 'somebody'
347+
? T extends 'once'
348+
? 'told'
349+
: 'me'
350+
: never;
351+
352+
declare function foo<T>(data: T): <S>(other: S) => Foo<T, S>;
353+
`,
354+
`
355+
type Foo<T, S> = S extends 'somebody'
356+
? T extends 'once'
357+
? 'told'
358+
: 'me'
359+
: never;
360+
361+
declare function foo<T>(data: T): <S>(other: S) => Foo<S, T>;
362+
`,
345363
`
346364
declare function mapObj<K extends string, V>(
347365
obj: { [key in K]?: V },

0 commit comments

Comments
 (0)