Rule proposal: no unnecessary type casting #250
Labels
package: eslint-plugin
Issues related to @typescript-eslint/eslint-plugin
triage
Waiting for team members to take a look
Casting to an assignable type is unnecessary. I would like to propose a rule that warns against it.
For instance:
or more commonly in return statements
I'm not exactly sure how to make this rule smart enough to not to be annoying. For instance, when using
Array.prototype.reduce
to construct a record, I always cast the initial value of type{}
toRecord<string, SOME_TYPE>
. This rule would warn against it:Assignable types are not "the same type", so I wonder if we should limit this rule to "to cast to exactly the same types". For instance an
as
cast here does not seem unnecessary:Of course we can use reduce's generic type argument to avoid the as cast. We can also use the return type declaration to avoid as cast in
foo()
. Maybe I can't find a good example if this rule can become annoying oras
casts are usually easy to avoid if types are assignable.Versions
@typescript-eslint/eslint-plugin
1.3.x
The text was updated successfully, but these errors were encountered: