microsoft / microsoft/TypeScript

Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled

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

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

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

説明

🔍 Search Terms

fallthrough noFallthroughCasesInSwitch

✅ Viability Checklist
⭐ Suggestion
    switch (...) {
        case 'A':
            doSomething();
            // fallthrough - this will allow falling to next statement
        case 'B':
            doSomethingElse();
   }

Instead of usage of @ts-expect-error this is nicely readable and intention is clearly visible in generated code.

📃 Motivating Example

Until now for noFallthroughCasesInSwitch is missing good way to allow falling through if needed. The only possible way @ts-expect-error is disabling not only fallthrough check, but also other checks which is not desired.

💻 Use Cases
  1. What do you want to use this for? Because eslint rule no-fallthrough does not handle TS exhaustive match, I would like to you noFallthroughCasesInSwitch config rule

  2. What shortcomings exist with current approaches?

    • eslint can not be used, its no-fallthrough rule can not detect exhaustive match, so the following code is incorrectly reported by eslint:
    function transform(action: 'KEEP' | 'INVERSE', b: boolean): boolean {
        switch (action) {
            case 'KEEP':
                switch (b) {
                    case true:
                        return true;
                    case false:
                        return false;
                }
            case 'INVERSE':
                switch (b) {
                    case true:
                        return false;
                    case false:
                        return true;
                }
        }
    }
    
    • satisfying eslint is not possible e.g. by putting break, because TS will start report Unreachable code detected and this can not be disabled by @ts-expect-error
    • disabling reported case by @ts-expect-error is also not viable, because it is disabling all type checks, so e.g. in case case something: validation that something has the correct type is also disabled (and issue #19139 is still opened)
  3. What workarounds are you using in the meantime? In my specific case, I had only one of these switches with exhaustive match, so I could put the statement as the last to not disable any check.

Implementation of #19139 would give me decent workaround and it is definitely more useful that this specific feature, but I believe both of them should be implemented.

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

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

はじめの一歩

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

調査の方向性

この issue ではファイルもテストも指定されていないため、まず TypeScript コンパイラーで noFallthroughCasesInSwitch 診断と switch の制御フローチェックを探します。提案されている fallthrough マーカーを、既存の @ts-expect-error および exhaustive-match のケースと比較し、意図的な fallthrough も引き続き型チェックされることを示す、対象を絞ったカバレッジを追加します。

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

評価

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

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

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