Skip to content

Commit 8ec05be

Browse files
authored
fix(eslint-plugin): [consistent-type-assertions] enforce assertionStyle for const assertions (typescript-eslint#4685)
1 parent 0fe0683 commit 8ec05be

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ export default util.createRule<Options, MessageIds>({
8484
function reportIncorrectAssertionType(
8585
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
8686
): void {
87+
const messageId = options.assertionStyle;
88+
8789
// If this node is `as const`, then don't report an error.
88-
if (isConst(node.typeAnnotation)) {
90+
if (isConst(node.typeAnnotation) && messageId === 'never') {
8991
return;
9092
}
9193

92-
const messageId = options.assertionStyle;
93-
9494
context.report({
9595
node,
9696
messageId,

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ const ruleTester = new RuleTester({
55
parser: '@typescript-eslint/parser',
66
});
77

8-
const ANGLE_BRACKET_TESTS = `
8+
const ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE = `
99
const x = <Foo>new Generic<int>();
1010
const x = <A>b;
1111
const x = <readonly number[]>[1];
12-
const x = <a | b>('string');
12+
const x = <a | b>('string');`;
13+
14+
const ANGLE_BRACKET_TESTS = `${ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE}
15+
const x = <const>{ key: 'value' };
1316
`;
14-
const AS_TESTS = `
17+
18+
const AS_TESTS_EXCEPT_CONST_CASE = `
1519
const x = new Generic<int>() as Foo;
1620
const x = b as A;
1721
const x = [1] as readonly number[];
18-
const x = ('string') as a | b;
22+
const x = ('string') as a | b;`;
23+
24+
const AS_TESTS = `${AS_TESTS_EXCEPT_CONST_CASE}
25+
const x = { key: 'value' } as const;
1926
`;
27+
2028
const OBJECT_LITERAL_AS_CASTS = `
2129
const x = {} as Foo<int>;
2230
`;
@@ -189,7 +197,7 @@ ruleTester.run('consistent-type-assertions', rule, {
189197
],
190198
}),
191199
...batchedSingleLineTests({
192-
code: AS_TESTS,
200+
code: AS_TESTS_EXCEPT_CONST_CASE,
193201
options: [
194202
{
195203
assertionStyle: 'never',
@@ -219,7 +227,7 @@ ruleTester.run('consistent-type-assertions', rule, {
219227
],
220228
}),
221229
...batchedSingleLineTests({
222-
code: ANGLE_BRACKET_TESTS,
230+
code: ANGLE_BRACKET_TESTS_EXCEPT_CONST_CASE,
223231
options: [
224232
{
225233
assertionStyle: 'never',

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