Skip to content

Commit f76a1b3

Browse files
authored
feat(eslint-plugin): [no-unnec-type-assertion] allow const assertions (typescript-eslint#1741)
1 parent 09d8afc commit f76a1b3

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const foo = <number>3;
4343
const foo = 3 as number;
4444
```
4545

46+
```ts
47+
const foo = 'foo' as const;
48+
```
49+
4650
```ts
4751
function foo(x: number | undefined): number {
4852
return x!;

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { TSESTree } from '@typescript-eslint/experimental-utils';
1+
import {
2+
TSESTree,
3+
AST_NODE_TYPES,
4+
} from '@typescript-eslint/experimental-utils';
25
import {
36
isObjectType,
47
isObjectFlagSet,
@@ -122,6 +125,14 @@ export default util.createRule<Options, MessageIds>({
122125
return false;
123126
}
124127

128+
function isConstAssertion(node: TSESTree.TypeNode): boolean {
129+
return (
130+
node.type === AST_NODE_TYPES.TSTypeReference &&
131+
node.typeName.type === AST_NODE_TYPES.Identifier &&
132+
node.typeName.name === 'const'
133+
);
134+
}
135+
125136
return {
126137
TSNonNullExpression(node): void {
127138
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);
@@ -201,7 +212,8 @@ export default util.createRule<Options, MessageIds>({
201212
if (
202213
options.typesToIgnore?.includes(
203214
sourceCode.getText(node.typeAnnotation),
204-
)
215+
) ||
216+
isConstAssertion(node.typeAnnotation)
205217
) {
206218
return;
207219
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@ function Test(props: {
130130
`,
131131
filename: 'react.tsx',
132132
},
133+
{
134+
code: `
135+
const a = [1, 2];
136+
const b = [3, 4];
137+
const c = [...a, ...b] as const;
138+
`,
139+
},
140+
{
141+
code: `const a = [1, 2] as const;`,
142+
},
143+
{
144+
code: `const a = 'a' as const;`,
145+
},
146+
{
147+
code: `const a = { foo: 'foo' } as const`,
148+
},
149+
{
150+
code: `
151+
const a = [1, 2];
152+
const b = [3, 4];
153+
const c = <const>[...a, ...b];
154+
`,
155+
},
156+
{
157+
code: `const a = <const>[1, 2];`,
158+
},
159+
{
160+
code: `const a = <const>'a';`,
161+
},
162+
{
163+
code: `const a = <const>{ foo: 'foo' };`,
164+
},
133165
],
134166

135167
invalid: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy