diff --git a/packages/eslint-plugin/docs/rules/no-unnecessary-template-expression.mdx b/packages/eslint-plugin/docs/rules/no-unnecessary-template-expression.mdx index c720652ebf7a..9262d9fea9be 100644 --- a/packages/eslint-plugin/docs/rules/no-unnecessary-template-expression.mdx +++ b/packages/eslint-plugin/docs/rules/no-unnecessary-template-expression.mdx @@ -67,6 +67,9 @@ enum ABC { } type ABCUnion = `${ABC}`; +// Interpolating type parameters is allowed. +type TextUtil = `${T}`; + const stringWithNumber = `1 + 1 = 2`; const stringWithBoolean = `true is true`; diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts b/packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts index 308855686d79..842f549eb34a 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts @@ -451,13 +451,14 @@ export default createRule<[], MessageId>({ isTrivialInterpolation(node) && !hasCommentsBetweenQuasi(node.quasis[0], node.quasis[1]) ) { - const { constraintType } = getConstraintInfo( + const { constraintType, isTypeParameter } = getConstraintInfo( checker, services.getTypeAtLocation(node.types[0]), ); if ( constraintType && + !isTypeParameter && isUnderlyingTypeString(constraintType) && !isEnumType(constraintType) ) { diff --git a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-template-expression.shot b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-template-expression.shot index 0065786d6ef9..2d5241b52e5e 100644 --- a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-template-expression.shot +++ b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-template-expression.shot @@ -60,6 +60,9 @@ enum ABC { } type ABCUnion = \`\${ABC}\`; +// Interpolating type parameters is allowed. +type TextUtil = \`\${T}\`; + const stringWithNumber = \`1 + 1 = 2\`; const stringWithBoolean = \`true is true\`; diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts index 160c9114f2aa..f60f87126f78 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts @@ -1190,6 +1190,12 @@ enum Foo { } type Bar = \`\${Foo.A}\`; `, + ` +function foo() { + const a: \`\${T}\` = 'a'; +} + `, + 'type T = `${A}`;', ], invalid: [ @@ -1431,26 +1437,5 @@ enum Foo { type Bar = Foo.A; `, }, - { - code: ` -function foo() { - const a: \`\${T}\` = 'a'; -} - `, - errors: [ - { - column: 13, - endColumn: 17, - endLine: 3, - line: 3, - messageId: 'noUnnecessaryTemplateExpression', - }, - ], - output: ` -function foo() { - const a: T = 'a'; -} - `, - }, ], });