Skip to content

Commit f1e9a5d

Browse files
authored
fix(eslint-plugin): [no-unnecessary-template-expression] handle template literal type (#10612)
* fix(eslint-plugin): handle expressions in template literal type * Update no-unnecessary-template-expression.test.ts * add docs * update snapshots * handle undefined & null * add tests * add tests * apply review * apply reviews * add comments
1 parent acab0a9 commit f1e9a5d

File tree

4 files changed

+601
-230
lines changed

4 files changed

+601
-230
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The new name is a drop-in replacement with identical functionality.
2828

2929
const ab1 = `${'a'}${'b'}`;
3030
const ab2 = `a${'b'}`;
31+
type AB1 = `${'A'}${'B'}`;
32+
type AB2 = `A${'B'}`;
3133

3234
const stringWithNumber = `${'1 + 1 = '}${2}`;
3335

@@ -38,9 +40,13 @@ const stringWithBoolean = `${'true is '}${true}`;
3840

3941
const text = 'a';
4042
const wrappedText = `${text}`;
43+
type Text = 'A';
44+
type WrappedText = `${Text}`;
4145

4246
declare const intersectionWithString: string & { _brand: 'test-brand' };
4347
const wrappedIntersection = `${intersectionWithString}`;
48+
type IntersectionWithString = string & { _brand: 'test-brand' };
49+
type WrappedIntersection = `${IntersectionWithString}`;
4450
```
4551

4652
</TabItem>
@@ -51,6 +57,15 @@ const wrappedIntersection = `${intersectionWithString}`;
5157

5258
const ab1 = `ab`;
5359
const ab2 = `ab`;
60+
type AB = `AB`;
61+
62+
// Transforming enum members into string unions using template literals is allowed.
63+
enum ABC {
64+
A = 'A',
65+
B = 'B',
66+
C = 'C',
67+
}
68+
type ABCUnion = `${ABC}`;
5469

5570
const stringWithNumber = `1 + 1 = 2`;
5671

@@ -61,9 +76,13 @@ const stringWithBoolean = `true is true`;
6176

6277
const text = 'a';
6378
const wrappedText = text;
79+
type Text = 'A';
80+
type WrappedText = Text;
6481

6582
declare const intersectionWithString: string & { _brand: 'test-brand' };
6683
const wrappedIntersection = intersectionWithString;
84+
type IntersectionWithString = string & { _brand: 'test-brand' };
85+
type WrappedIntersection = IntersectionWithString;
6786
```
6887

6988
</TabItem>

0 commit comments

Comments
 (0)