Skip to content

fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped alias type arguments #9741

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
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ function collectTypeParameterUsageCounts(
}
}

// Catch-all: generic type references like `Exclude<T, null>`
else if (type.aliasTypeArguments) {
// We don't descend into the definition of the type alias, so we don't
// know whether it's used multiple times. It's safest to assume it is.
visitTypesList(type.aliasTypeArguments, true);
}

// Intersections and unions like `0 | 1`
else if (tsutils.isUnionOrIntersectionType(type)) {
visitTypesList(type.types, assumeMultipleUses);
Expand Down Expand Up @@ -307,10 +314,6 @@ function collectTypeParameterUsageCounts(
}
}

for (const typeArgument of type.aliasTypeArguments ?? []) {
visitType(typeArgument, true);
}

visitType(type.getNumberIndexType(), true);
visitType(type.getStringIndexType(), true);

Expand All @@ -329,11 +332,6 @@ function collectTypeParameterUsageCounts(
else if (isOperatorType(type)) {
visitType(type.type, assumeMultipleUses);
}

// Catch-all: generic type references like `Exclude<T, null>`
else if (type.aliasTypeArguments) {
visitTypesList(type.aliasTypeArguments, true);
}
}

function incrementIdentifierCount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ ruleTester.run('no-unnecessary-type-parameters', rule, {
(token): token is Exclude<TSESTree.Token, Conditions & ExtractedToken> =>
tokenType in conditions;
`,
`
type Foo<T, S> = S extends 'somebody'
? T extends 'once'
? 'told'
: 'me'
: never;

declare function foo<T>(data: T): <S>(other: S) => Foo<T, S>;
`,
`
type Foo<T, S> = S extends 'somebody'
? T extends 'once'
? 'told'
: 'me'
: never;

declare function foo<T>(data: T): <S>(other: S) => Foo<S, T>;
`,
`
declare function mapObj<K extends string, V>(
obj: { [key in K]?: V },
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy