microsoft / microsoft/TypeScript

Report better errors on return expressions for immediately-assigned functions

オープン
#42,161 コメント 6 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

Domain: Error Messages Effort: Moderate Experience Enhancement Experimentation Needed Help Wanted Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

🔍 Search Terms

return type label:"Domain: Error Messages"

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Error reporting for mismatches between a functions return type and what is being returned by a particular return statement should be consistently reported a the location of the offending return.

📃 Motivating Example

TypeScript Playground

type Foo = (input: string) => number;

const foo: Foo = (input) => {
  switch (input) {
    case "1":
      return 1;
    case "2":
      return "2";
    default:
      return Infinity;
  }
}

The following error is reported on the declaration of foo.

const foo: Foo
Type '(input: string) => number | "2"' is not assignable to type 'Foo'.
  Type 'number | "2"' is not assignable to type 'number'.
    Type 'string' is not assignable to type 'number'.(2322)
const bar = (input: string): number => {
  switch (input) {
    case "1":
      return 1;
    case "2":
      return "2";
    default:
      return Infinity;
  }
}

The following error is reported on return "2":

Type 'string' is not assignable to type 'number'.(2322)

💻 Use Cases

In the second example it's much easier to see where the problem is. In larger functions for more `returns it can be even hard to pin down which one is causing the issue. These two examples are very similar and I would expect the same error to be reported for each.

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

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

はじめの一歩

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

調査の方向性

動機となる TypeScript の例とリンクされた Playground から始め、すぐに代入される関数と、戻り値の型が明示的に注釈付けされた関数の診断を比較します。最初の形式での戻り値の型の不一致が、2 番目の例と一貫して、問題のある return 文で報告され、出力される JavaScript が変更されなければ完了です。

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

評価

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

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

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