Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 8a34c3d

Browse files
committed
Add rule @typescript-eslint/consistent-type-assertions
1 parent 2c29025 commit 8a34c3d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type { RuleConfig } from '../rule-config';
2+
3+
/**
4+
* Option.
5+
*/
6+
export type ConsistentTypeAssertionsOption =
7+
| {
8+
/* Empty option */
9+
}
10+
| {
11+
/**
12+
* This option defines the expected assertion style.
13+
*
14+
* @default 'as'
15+
*
16+
* @see [assertionStyle](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md#assertionstyle)
17+
*/
18+
assertionStyle: 'as' | 'angle-bracket';
19+
/**
20+
* Always prefer `const x: T = { ... };` to `const x = { ... } as T;` (or similar with angle brackets).
21+
*
22+
* The type assertion in the latter case is either unnecessary or will probably hide an error.
23+
*
24+
* @default 'allow'
25+
*
26+
* @see [objectLiteralTypeAssertions](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md#objectliteraltypeassertions)
27+
*/
28+
objectLiteralTypeAssertions?: 'allow' | 'allow-as-parameter' | 'never';
29+
}
30+
| {
31+
/**
32+
* This option defines the expected assertion style.
33+
*
34+
* @see [assertionStyle](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md#assertionstyle)
35+
*/
36+
assertionStyle: 'never';
37+
};
38+
39+
/**
40+
* Options.
41+
*/
42+
export type ConsistentTypeAssertionsOptions = [ConsistentTypeAssertionsOption?];
43+
44+
/**
45+
* Enforces consistent usage of type assertions.
46+
*
47+
* @see [consistent-type-assertions](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md)
48+
*/
49+
export type ConsistentTypeAssertionsRuleConfig = RuleConfig<ConsistentTypeAssertionsOptions>;
50+
51+
/**
52+
* Enforces consistent usage of type assertions.
53+
*
54+
* @see [consistent-type-assertions](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md)
55+
*/
56+
export interface ConsistentTypeAssertionsRule {
57+
/**
58+
* Enforces consistent usage of type assertions.
59+
*
60+
* @see [consistent-type-assertions](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/consistent-type-assertions.md)
61+
*/
62+
'@typescript-eslint/consistent-type-assertions': ConsistentTypeAssertionsRuleConfig;
63+
}

src/rules/typescript-eslint/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ArrayTypeRule } from './array-type';
33
import type { AwaitThenableRule } from './await-thenable';
44
import type { BanTsCommentRule } from './ban-ts-comment';
55
import type { BanTypesRule } from './ban-types';
6+
import type { ConsistentTypeAssertionsRule } from './consistent-type-assertions';
67
import type { ExplicitFunctionReturnTypeRule } from './explicit-function-return-type';
78
import type { InterfaceNamePrefixRule } from './interface-name-prefix';
89
import type { MemberOrderingRule } from './member-ordering';
@@ -25,6 +26,7 @@ export type TypeScriptEslintRules = AdjacentOverloadSignaturesRule &
2526
AwaitThenableRule &
2627
BanTsCommentRule &
2728
BanTypesRule &
29+
ConsistentTypeAssertionsRule &
2830
ExplicitFunctionReturnTypeRule &
2931
InterfaceNamePrefixRule &
3032
MemberOrderingRule &

0 commit comments

Comments
 (0)