Skip to content

Rule proposal: no unnecessary type casting #250

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

Closed
mohsen1 opened this issue Feb 11, 2019 · 2 comments
Closed

Rule proposal: no unnecessary type casting #250

mohsen1 opened this issue Feb 11, 2019 · 2 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Feb 11, 2019

Casting to an assignable type is unnecessary. I would like to propose a rule that warns against it.

For instance:

let a: string = 'str' as string;

or more commonly in return statements

interface Foo { bar: string }

function foo(): Foo {
  return { bar: 'str' } as Foo;
}

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 {} to Record<string, SOME_TYPE>. This rule would warn against it:

[1,2,3].reduce((acc, item, index) => { 
    acc[index] = item
    return acc;
}, {} as Record<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 an as cast here does not seem unnecessary:

interface Foo { bar: string; baz?: number }

function foo() {
  return { bar: 'str' } as Foo
}

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 or as casts are usually easy to avoid if types are assignable.

Versions

package version
@typescript-eslint/eslint-plugin 1.3.x
@mohsen1 mohsen1 added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 11, 2019
@JamesHenry
Copy link
Member

A "cast" is technically called a type assertion, and we already do have a rule for this:

https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin

@typescript-eslint/no-unnecessary-type-assertion

@JamesHenry
Copy link
Member

Rule README.md https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.md

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

2 participants