microsoft / microsoft/TypeScript

Codefix: removing inferable types

オープン
#35,691 コメント 1 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Domain: LS: Quick Fixes Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

inferable types no-inferable-types no-inferrable-types removal

Suggestion

It is somewhat unnecessary to add : type declarations for member, parameter, and/or variable types already inferable from code.

// This type declaration actually requests a lower amount of type information,
// as `number` is less specific (narrow) than `3`
const value: number = 3;

It'd be nice to have a codefix to remove them to clean up code.

Use Cases

These unnecessary type declarations can easily get introduced in code as it gets changing during editing, and can cause visual clutter.

We should note that some users choose to prefer including these inferable type declarations as a style preference to increase the amount of type information visible purely from source code.

The @typescript-eslint/no-inferable-types already exists with an auto-fixer for a good starting reference.

Examples

Variables such as the above value can generally have their type removed if either is true:

  • The variable is let, contains an initializer, and the type is the same as what it would be inferred as

    // We can remove `: number` here
    let value: number = 0;
    
    // We cannot remove `: number | string`, as `| string` is not added by TypeScript
    let otherValue: number | string = 0;
    
  • The variable is const and the type is less narrow than what the type is inferred as

Parameters can similarly be treated as let parameters:

// The `: string` can be safely removed
function takesStringWithDefault(input: string = "") { }

Parameters can also have type declarations removed if their parent function is already declared to be a type that provides the same type declaration for the parameter:

type TakesString = (input: string) => void;

// We can remove the `: string` here too
const takesString: TakesString = (input: string) => {};

Class members can be treated as let variables here too.

class WithValue {
    // We can remove the `: string` here
    value: string = "";
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue では、リポジトリのファイル、テスト、エントリーポイントが指定されていません。まず変数、パラメーター、クラスメンバーの例を確認し、次に提案された動作を @typescript-eslint/no-inferable-types の auto-fixer と比較してください。完了とは、runtime output を変更せずに、説明された inferable annotations だけを安全に削除する codefix が実装されていることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。