Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
Casting to an assignable type is unnecessary. I would like to propose a rule that warns against it.
For instance:
leta:string='str'asstring;
or more commonly in return statements
interfaceFoo{bar:string}functionfoo():Foo{return{bar:'str'}asFoo;}
I'm not exactly sure how to make this rule smart enough to not to be annoying. For instance, when usingArray.prototype.reduce
to construct a record, I always cast the initial value of type{}
toRecord<string, SOME_TYPE>
. This rule would warn against it:
[1,2,3].reduce((acc,item,index)=>{acc[index]=itemreturnacc;},{}asRecord<string,number>)
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 anas
cast here does not seem unnecessary:
interfaceFoo{bar:string;baz?:number}functionfoo(){return{bar:'str'}asFoo}
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 infoo()
. 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
package | version |
---|---|
@typescript-eslint/eslint-plugin | 1.3.x |