microsoft / microsoft/TypeScript

Improve error message for incompatible signatures in union type from typed/untyped function call

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

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

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

説明

Search Terms

untyped function call error message union type incompatible signature

Example

This code errors as follows:

function foo<T>(x: T | (() => string)) {
    if (typeof x === "function") {
        let a = x();
             // ^ Cannot invoke an expression whose type lacks a call signature. 
             //Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
    }
}

which feels strange given that both of the following are fine:

function fooL<T>(x: T) {
    if (typeof x === "function") {
        let a = x();
    }
}

function fooR(x: () => string) {
    if (typeof x === "function") {
        let a = x();
    }
}

If an evaluation contexts accepts values of type A and values of type B, then it should accepts values of type A | B. I think what is going on is that the call signature from T & Function is untyped, which is incompatible with the typed signature of () => string.

I don't think this is very obvious to a user, and they end up seeing an application that works for both branches of a union, but not their composition.

Suggestion

I think making the example work is out of scope. My suggestion is to improve the error message, something like:

function foo<T>(x: T | (() => string)) {
    if (typeof x === "function") {
        let a = x();
             // ^ Cannot invoke an expression whose type lacks a call signature. 
             // Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
             //   Cannot combine untyped function call with typed function call
    }
}

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. new expression-level syntax)

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

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

はじめの一歩

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

調査の方向性

まず issue の TypeScript の例を実行し、コンパイラー診断でエラー 2349 を検索します。union の call signature が現在のメッセージをどのように生成するかを追跡します。互換性のない typed および untyped の関数呼び出しが、より明確で説明的な診断を受け、既存の例も引き続きカバーされれば完了です。

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

評価

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

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

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