Skip to content

Commit 77df70d

Browse files
authored
fix(eslint-plugin): [no-unnecessary-template-expression] allow interpolating type parameter in type context (#10739)
* fix(eslint-plugin): [no-unnecessary-template-expression] allow type parameter * update
1 parent 8376abe commit 77df70d

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

packages/eslint-plugin/docs/rules/no-unnecessary-template-expression.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ enum ABC {
6767
}
6868
type ABCUnion = `${ABC}`;
6969

70+
// Interpolating type parameters is allowed.
71+
type TextUtil<T extends string> = `${T}`;
72+
7073
const stringWithNumber = `1 + 1 = 2`;
7174

7275
const stringWithBoolean = `true is true`;

packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,14 @@ export default createRule<[], MessageId>({
451451
isTrivialInterpolation(node) &&
452452
!hasCommentsBetweenQuasi(node.quasis[0], node.quasis[1])
453453
) {
454-
const { constraintType } = getConstraintInfo(
454+
const { constraintType, isTypeParameter } = getConstraintInfo(
455455
checker,
456456
services.getTypeAtLocation(node.types[0]),
457457
);
458458

459459
if (
460460
constraintType &&
461+
!isTypeParameter &&
461462
isUnderlyingTypeString(constraintType) &&
462463
!isEnumType(constraintType)
463464
) {

packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-template-expression.shot

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts

+6-21
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,12 @@ enum Foo {
11901190
}
11911191
type Bar = \`\${Foo.A}\`;
11921192
`,
1193+
`
1194+
function foo<T extends string>() {
1195+
const a: \`\${T}\` = 'a';
1196+
}
1197+
`,
1198+
'type T<A extends string> = `${A}`;',
11931199
],
11941200

11951201
invalid: [
@@ -1431,26 +1437,5 @@ enum Foo {
14311437
type Bar = Foo.A;
14321438
`,
14331439
},
1434-
{
1435-
code: `
1436-
function foo<T extends string>() {
1437-
const a: \`\${T}\` = 'a';
1438-
}
1439-
`,
1440-
errors: [
1441-
{
1442-
column: 13,
1443-
endColumn: 17,
1444-
endLine: 3,
1445-
line: 3,
1446-
messageId: 'noUnnecessaryTemplateExpression',
1447-
},
1448-
],
1449-
output: `
1450-
function foo<T extends string>() {
1451-
const a: T = 'a';
1452-
}
1453-
`,
1454-
},
14551440
],
14561441
});

0 commit comments

Comments
 (0)