8000 feat(eslint-plugin): add option to ignore string const assertiions in… · KuSh/typescript-eslint@9eeaafe · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eeaafe

Browse files
committed
feat(eslint-plugin): add option to ignore string const assertiions in no-unnecessary-type-assertion rule
Fixes typescript-eslint#8721
1 parent 6b64ac4 commit 9eeaafe

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ function foo(x: number | undefined): number {
7373

7474
## Options
7575

76+
### `ignoreStringConstAssertion`
77+
78+
{/* insert option description */}
79+
80+
With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { ignoreStringConstAssertion: true }]`, the following is **correct** code:
81+
82+
```ts option='{ "ignoreStringConstAssertion": true }' showPlaygroundButton
83+
const foo = `foo` as const;
84+
```
85+
7686
### `typesToIgnore`
7787

7888
{/* insert option description */}

packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121

2222
export type Options = [
2323
{
24+
ignoreStringConstAssertion?: boolean;
2425
typesToIgnore?: string[];
2526
},
2627
];
@@ -48,6 +49,10 @@ export default createRule<Options, MessageIds>({
4849
type: 'object',
4950
additionalProperties: false,
5051
properties: {
52+
ignoreStringConstAssertion: {
53+
type: 'boolean',
54+
description: 'Whether to ignore string const assertions.',
55+
},
5156
typesToIgnore: {
5257
type: 'array',
5358
description: 'A list of type names to ignore.',
@@ -233,6 +238,14 @@ export default createRule<Options, MessageIds>({
233238
const uncastType = services.getTypeAtLocation(node.expression);
234239
const typeIsUnchanged = isTypeUnchanged(uncastType, castType);
235240

241+
if (
242+
options.ignoreStringConstAssertion &&
243+
uncastType.isStringLiteral() &&
244+
isConstAssertion(node.typeAnnotation)
245+
) {
246+
return;
247+
}
248+
236249
const wouldSameTypeBeInferred = castType.isLiteral()
237250
? isImplicitlyNarrowedLiteralDeclaration(node)
238251
: !isConstAssertion(node.typeAnnotation);

packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-unnecessary-type-assertion.shot

Lines changed: 7 additions & 0 deletions
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-type-assertion.test.ts

Lines changed: 12 additions & 0 deletions

packages/eslint-plugin/tests/schema-snapshots/no-unnecessary-type-assertion.shot

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,18 @@ declare function foo<T extends unknown>(bar: T): T;
429429
const baz: unknown = {};
430430
foo(baz!);
431431
`,
432+
{
433+
code: 'const a = `a` as const;',
434+
options: [{ ignoreStringConstAssertion: true }],
435+
},
436+
{
437+
code: "const a = 'a' as const;",
438+
options: [{ ignoreStringConstAssertion: true }],
439+
},
440+
{
441+
code: "const a = <const>'a';",
442+
options: [{ ignoreStringConstAssertion: true }],
443+
},
432444
],
433445

434446
invalid: [