Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53dc34d

Browse files
authored
fix(eslint-plugin): [non-nullable-type-assertion-style] handle const assertion (typescript-eslint#2881)
1 parent d35a539 commit 53dc34d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { TSESTree } from '@typescript-eslint/experimental-utils';
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
25
import * as tsutils from 'tsutils';
36
import * as ts from 'typescript';
47

@@ -69,10 +72,24 @@ export default util.createRule({
6972
return true;
7073
};
7174

75+
const isConstAssertion = (
76+
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
77+
): boolean => {
78+
return (
79+
node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference &&
80+
node.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier &&
81+
node.typeAnnotation.typeName.name === 'const'
82+
);
83+
};
84+
7285
return {
7386
'TSAsExpression, TSTypeAssertion'(
7487
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
7588
): void {
89+
if (isConstAssertion(node)) {
90+
return;
91+
}
92+
7693
const originalTypes = getTypesIfNotLoose(node.expression);
7794
if (!originalTypes) {
7895
return;

packages/eslint-plugin/tests/rules/non-nullable-type-assertion-style.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ declare const x: T | number;
5151
5252
const y = x as NonNullable<T>;
5353
`,
54+
`
55+
const foo = [] as const;
56+
`,
5457
],
5558

5659
invalid: [

0 commit comments

Comments
 (0)