Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-template-expression] allow interp…
Browse files Browse the repository at this point in the history
…olating type parameter in type context (#10739)

* fix(eslint-plugin): [no-unnecessary-template-expression] allow type parameter

* update
  • Loading branch information
yeonjuan authored Feb 3, 2025
1 parent 8376abe commit 77df70d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ enum ABC {
}
type ABCUnion = `${ABC}`;

// Interpolating type parameters is allowed.
type TextUtil<T extends string> = `${T}`;

const stringWithNumber = `1 + 1 = 2`;

const stringWithBoolean = `true is true`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,12 @@ enum Foo {
}
type Bar = \`\${Foo.A}\`;
`,
`
function foo<T extends string>() {
const a: \`\${T}\` = 'a';
}
`,
'type T<A extends string> = `${A}`;',
],

invalid: [
Expand Down Expand Up @@ -1431,26 +1437,5 @@ enum Foo {
type Bar = Foo.A;
`,
},
{
code: `
function foo<T extends string>() {
const a: \`\${T}\` = 'a';
}
`,
errors: [
{
column: 13,
endColumn: 17,
endLine: 3,
line: 3,
messageId: 'noUnnecessaryTemplateExpression',
},
],
output: `
function foo<T extends string>() {
const a: T = 'a';
}
`,
},
],
});

0 comments on commit 77df70d

Please sign in to comment.