microsoft / microsoft/TypeScript

Show incompatible union members in discriminated union discriminator errors

Offen
#62,737 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Experimentation Needed Help Wanted Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem motivierenden Beispiel einer discriminated union und vergleiche die aktuelle Diagnose mit der vorgeschlagenen. Verfolge anschließend den Pfad des Type-Checkers für Zuweisbarkeit und Fehler bei Discriminators von discriminated unions; überprüfe dann, dass das implementierte Verhalten inkompatible Mitglieder der Source-Union wie "baz" identifiziert, ohne die Laufzeitausgabe zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.