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

Skip to content

Commit af2c00d

Browse files
authored
feat(eslint-plugin): [consistent-type-assertions] always allow const assertions (typescript-eslint#1713)
1 parent 62b2278 commit af2c00d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/eslint-plugin/docs/rules/consistent-type-assertions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Type assertions are also commonly referred as "type casting" in TypeScript (even
88

99
In addition to ensuring that type assertions are written in a consistent way, this rule also helps make your codebase more type-safe.
1010

11+
`const` assertions, [introduced in TypeScript 3.4](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions), is always allowed by this rule. Examples of it include `let x = "hello" as const;` and `let x = <const>"hello";`.
12+
1113
## Options
1214

1315
```ts

packages/eslint-plugin/src/rules/consistent-type-assertions.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,27 @@ export default util.createRule<Options, MessageIds>({
7575
create(context, [options]) {
7676
const sourceCode = context.getSourceCode();
7777

78+
function isConst(node: TSESTree.TypeNode): boolean {
79+
if (node.type !== AST_NODE_TYPES.TSTypeReference) {
80+
return false;
81+
}
82+
83+
return (
84+
node.typeName.type === AST_NODE_TYPES.Identifier &&
85+
node.typeName.name === 'const'
86+
);
87+
}
88+
7889
function reportIncorrectAssertionType(
7990
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
8091
): void {
92+
// If this node is `as const`, then don't report an error.
93+
if (isConst(node.typeAnnotation)) {
94+
return;
95+
}
96+
8197
const messageId = options.assertionStyle;
98+
8299
context.report({
83100
node,
84101
messageId,
@@ -97,8 +114,7 @@ export default util.createRule<Options, MessageIds>({
97114
case AST_NODE_TYPES.TSTypeReference:
98115
return (
99116
// Ignore `as const` and `<const>`
100-
(node.typeName.type === AST_NODE_TYPES.Identifier &&
101-
node.typeName.name !== 'const') ||
117+
!isConst(node) ||
102118
// Allow qualified names which have dots between identifiers, `Foo.Bar`
103119
node.typeName.type === AST_NODE_TYPES.TSQualifiedName
104120
);

packages/eslint-plugin/tests/rules/consistent-type-assertions.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ const ANGLE_BRACKET_TESTS = `
99
const x = <Foo>new Generic<int>();
1010
const x = <A>b;
1111
const x = <readonly number[]>[1];
12-
const x = <const>[1];
1312
const x = <a | b>('string');
1413
`;
1514
const AS_TESTS = `
1615
const x = new Generic<int>() as Foo;
1716
const x = b as A;
1817
const x = [1] as readonly number[];
19-
const x = [1] as const;
2018
const x = ('string') as a | b;
2119
`;
2220
const OBJECT_LITERAL_AS_CASTS = `
@@ -98,6 +96,22 @@ ruleTester.run('consistent-type-assertions', rule, {
9896
},
9997
],
10098
}),
99+
{
100+
code: 'const x = <const>[1];',
101+
options: [
102+
{
103+
assertionStyle: 'never',
104+
},
105+
],
106+
},
107+
{
108+
code: 'const x = [1] as const;',
109+
options: [
110+
{
111+
assertionStyle: 'never',
112+
},
113+
],
114+
},
101115
],
102116
invalid: [
103117
...batchedSingleLineTests({

0 commit comments

Comments
 (0)