Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: no unnecessary type casting #250

Closed
Labels
package: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look
@mohsen1

Description

@mohsen1

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

packageversion
@typescript-eslint/eslint-plugin1.3.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp