From 527c5fa2ce9b2ba2d6c5dcd3a716308c1d59619e Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Aug 2024 15:28:06 -0400 Subject: [PATCH] fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped constraint types if necessary --- .../rules/no-unnecessary-type-parameters.ts | 2 +- .../no-unnecessary-type-parameters.test.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts b/packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts index f191efa2f4e7..7b5d7de33c4a 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts @@ -280,7 +280,7 @@ function collectTypeParameterUsageCounts( if (properties.length === 0) { // TS treats mapped types like `{[k in "a"]: T}` like `{a: T}`. // They have properties, so we need to avoid double-counting. - visitType(type.templateType, false); + visitType(type.templateType ?? type.constraintType, false); } } diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-parameters.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-parameters.test.ts index e5a7558e2f49..aff4d173ed83 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-parameters.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-parameters.test.ts @@ -348,6 +348,33 @@ ruleTester.run('no-unnecessary-type-parameters', rule, { fn: (key: K, val: V) => number, ): number[]; `, + ` + declare function mappedReturnType( + x: T, + ): { [K in T]: Capitalize }; + + function inferredMappedReturnType(x: T) { + return mappedReturnType(x); + } + `, + ` + declare function mappedReturnType( + x: T, + ): { [K in T]: Capitalize }; + + function inferredMappedReturnType(x: T) { + return () => mappedReturnType(x); + } + `, + ` + declare function mappedReturnType( + x: T, + ): { [K in T]: Capitalize }; + + function inferredMappedReturnType(x: T) { + return [{ value: () => mappedReturnType(x) }]; + } + `, ], invalid: [ 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