microsoft / microsoft/TypeScript

Show incompatible union members in discriminated union discriminator errors

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

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

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

説明

🔍 Search Terms

discriminated union error message, union type assignability error, misleading error message, discriminator property, wider union error, union literal type error, type narrowing error message

✅ Viability Checklist
⭐ Suggestion

Improve error messages when assigning objects with wider discriminator unions to discriminated union types. Currently, TypeScript reports the compatible literal as incompatible, rather than identifying the actual problematic values in the wider union that prevent assignment.

📃 Motivating Example

Consider this code [Playground Link]:

 type Discriminated =
    | { discriminator: "foo" }
    | { discriminator: "bar" };

  type UnionType = "foo" | "bar" | "baz";

  const obj = { discriminator: "foo" as UnionType };

  // Error: Type '{ discriminator: UnionType; }' is not assignable to type 'Discriminated'.
  //   Type '{ discriminator: UnionType; }' is not assignable to type '{ discriminator: "bar"; barValue: number; }'.
  //     Types of property 'discriminator' are incompatible.
  //       Type 'UnionType' is not assignable to type '"bar"'.
  //         Type '"foo"' is not assignable to type '"bar"'.
  const err: Discriminated = obj;

Current error message: Type '"foo"' is not assignable to type '"bar"'

Problem: This error is misleading. "foo" is compatible with the discriminated union. The real issue is that "baz" (present in UnionType) is
not compatible with any variant of Discriminated.

Better error message (proposed):
Type 'UnionType' is not assignable to the discriminator type '"foo" | "bar"'.
Type(s) '"baz"' from the source union are not assignable to the target.

This directly identifies "baz" as the problematic value, making it immediately clear what needs to be fixed.

💻 Use Cases
  1. What do you want to use this for?

When working with discriminated unions in large codebases, developers frequently encounter situations where:

  • API responses have wider string union types than the application's internal types
  • Type narrowing is attempted from generic to specific discriminated unions
  • Configuration objects use string unions that need to match discriminated union variants

Better error messages would significantly reduce debugging time by directly identifying incompatible discriminator values instead of
misleading developers toward compatible ones.

  1. What shortcomings exist with current approaches?

The current error message:

  • Points to a compatible literal type as the source of error
  • Requires developers to manually compare all members of both unions to identify the actual problem
  • Creates confusion, especially for TypeScript learners
  • Makes the error difficult to search for and understand in complex type hierarchies
  1. What workarounds are you using in the meantime?

Current workarounds include:

  • Manually extracting and comparing union members using type utilities
  • Adding explicit type assertions with as to silence the error (unsafe)
  • Creating intermediate types with explicit exclusions: Exclude<UnionType, "baz">
  • Trial-and-error removal of union members until the error clears

All of these approaches are suboptimal compared to a clear, actionable error message.


Note: This suggestion stems from issue https://github.com/microsoft/TypeScript/issues/62603

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

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

はじめの一歩

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

調査の方向性

動機となる discriminated union の例から始め、現在の診断結果と提案されている診断結果を比較します。次に、assignability と discriminated union の discriminator エラーに関する type-checker の処理経路を追跡し、最後に、実装された動作によって "baz" などの互換性のない source-union メンバーが特定され、runtime の出力が変わらないことを確認します。

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

評価

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

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

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