-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
79 changes: 79 additions & 0 deletions
79
packages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.mdx
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 @@ | ||
--- | ||
description: 'Disallow conversion idioms when they do not change the type or value of the expression.' | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-unnecessary-type-conversion** for documentation. | ||
|
||
JavaScript provides several commonly used idioms to convert values to a specific type: | ||
|
||
- Primitive coercion (e.g. `Boolean(value)`, `String(value)`): using a built-in primitive function | ||
- String concatenation (e.g. `value + ''`): turning a value into a string | ||
- Unary coercion (e.g. `+value`, `!!value`): using a built-in operator | ||
- The `.toString()` method defined on many types | ||
|
||
These conversions are unnecessary if the value is already of that type. | ||
|
||
## Examples | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
String('123'); | ||
'123'.toString(); | ||
'' + '123'; | ||
'123' + ''; | ||
|
||
Number(123); | ||
+123; | ||
~~123; | ||
|
||
Boolean(true); | ||
!!true; | ||
|
||
BigInt(BigInt(1)); | ||
|
||
let str = '123'; | ||
str += ''; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
function foo(bar: string | number) { | ||
String(bar); | ||
bar.toString(); | ||
'' + bar; | ||
bar + ''; | ||
|
||
Number(bar); | ||
+bar; | ||
~~bar; | ||
|
||
Boolean(bar); | ||
!!bar; | ||
|
||
BigInt(1); | ||
|
||
bar += ''; | ||
} | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about having no-op type conversions in your code, then you can turn off this rule. | ||
If you have types which are not accurate, then this rule might cause you to remove conversions that you actually do need. | ||
|
||
## Related To | ||
|
||
- [no-unnecessary-type-assertion](./no-unnecessary-type-assertion.mdx) | ||
- [no-useless-template-literals](./no-useless-template-literals.mdx) |
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
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
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
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.
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.
feat(eslint-plugin): [no-unnecessary-type-conversion] add rule #10182
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
feat(eslint-plugin): [no-unnecessary-type-conversion] add rule #10182
Changes from all commits
762cd47
3060bff
393fb2f
00476f9
89ecd74
879dd41
1638cb3
0112f0a
bfd8c5b
05f4c6e
< 8000 div class="text-emphasized css-truncate css-truncate-target"> Merge branch 'real_main'e125bb9
0a1257b
0dd5d52
b054d60
aac5d40
1029658
988f383
606556e
6691fc7
6033707
d5b9f2f
7568840
251bb05
0b12c0e
c85bf9a
2877d5f
178b961
a89e6ca
74bf8a1
17dfbc9
6afafe9
c0976d1
973234f
e8956c0
ad88ade
36cf822
8539069
2a1471f
4a56a42
94ebd1e
0d16798
ae8f471
09b14f8
6f88f00
e2c037a
6822a69
File filter
Filter by extension
Conversations
Jump to