-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin)!: add rule consistent-type-assertions
#731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
720b624
feat(eslint-plugin): add rule `consistent-type-assertions`
bradzacher a0d5762
chore: remove deprecated rules, update readme
bradzacher ca96941
Merge branch 'master' into consistent-type-assertions
bradzacher d118613
fix: update recommended so lint passes
bradzacher cb859a1
Merge branch 'master' into consistent-type-assertions
bradzacher ed22138
Merge branch 'master' into consistent-type-assertions
bradzacher b123512
Merge branch 'master' into consistent-type-assertions
bradzacher 162e38d
Merge branch 'master' into consistent-type-assertions
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/eslint-plugin/docs/rules/consistent-type-assertions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Enforces consistent usage of type assertions. (consistent-type-assertions) | ||
|
||
## Rule Details | ||
|
||
This rule aims to standardise the use of type assertion style across the codebase. | ||
|
||
Type assertions are also commonly referred as "type casting" in TypeScript (even though it is technically slightly different to what is understood by type casting in other languages), so you can think of type assertions and type casting referring to the same thing. It is essentially you saying to the TypeScript compiler, "in this case, I know better than you!". | ||
|
||
## Options | ||
|
||
```ts | ||
type Options = | ||
| { | ||
assertionStyle: 'as' | 'angle-bracket'; | ||
objectLiteralTypeAssertions: 'allow' | 'allow-as-parameter' | 'never'; | ||
} | ||
| { | ||
assertionStyle: 'never'; | ||
}; | ||
|
||
const defaultOptions: Options = { | ||
assertionStyle: 'as', | ||
objectLiteralTypeAssertions: 'allow', | ||
}; | ||
``` | ||
|
||
### assertionStyle | ||
|
||
This option defines the expected assertion style. Valid values for `assertionStyle` are: | ||
|
||
- `as` will enforce that you always use `... as foo`. | ||
- `angle-bracket` will enforce that you always use `<foo>...` | ||
- `never` will enforce that you do not do any type assertions. | ||
|
||
Most code bases will want to enforce not using `angle-bracket` style because it conflicts with JSX syntax, and is confusing when paired with with generic syntax. | ||
|
||
Some codebases like to go for an extra level of type safety, and ban assertions altogether via the `never` option. | ||
|
||
### objectLiteralTypeAssertions | ||
|
||
Always prefer `const x: T = { ... };` to `const x = { ... } as T;` (or similar with angle brackets). The type assertion in the latter case is either unnecessary or will probably hide an error. | ||
|
||
The compiler will warn for excess properties with this syntax, but not missing _required_ fields. For example: `const x: { foo: number } = {};` will fail to compile, but `const x = {} as { foo: number }` will succeed. | ||
|
||
The const assertion `const x = { foo: 1 } as const`, introduced in TypeScript 3.4, is considered beneficial and is ignored by this option. | ||
|
||
Examples of **incorrect** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }` (and for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`) | ||
|
||
```ts | ||
const x = { ... } as T; | ||
``` | ||
|
||
Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'never' }`. | ||
|
||
```ts | ||
const x: T = { ... }; | ||
const y = { ... } as any; | ||
const z = { ... } as unknown; | ||
``` | ||
|
||
Examples of **correct** code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' }`. | ||
|
||
```ts | ||
const x: T = { ... }; | ||
const y = { ... } as any; | ||
const z = { ... } as unknown; | ||
foo({ ... } as T); | ||
new Clazz({ ... } as T); | ||
function foo() { throw { bar: 5 } as Foo } | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce consistent type assertions. | ||
|
||
## Compatibility | ||
|
||
- TSLint: [no-angle-bracket-type-assertion](https://palantir.github.io/tslint/rules/no-angle-bracket-type-assertion/) | ||
- TSLint: [no-object-literal-type-assertion](https://palantir.github.io/tslint/rules/no-object-literal-type-assertion/) |
32 changes: 0 additions & 32 deletions
32
packages/eslint-plugin/docs/rules/no-angle-bracket-type-assertion.md
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
packages/eslint-plugin/docs/rules/no-object-literal-type-assertion.md
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.