Microsoft/TypeScript

Misleading error message for missing properties in types composed of the intersection of certain multiple unions

Open

#53.200 geöffnet am 10. März 2023

Auf GitHub ansehen
 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Experience EnhancementHelp WantedSuggestion

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

🔎 Search Terms

intersection of union required property error message

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about it

I use the playground and observed the same behavior for versions 4.2 through 4.9

⏯ Playground Link

Playground link with relevant code

💻 Code

type Arg =  (
  | {
      prefLabel?: string;
      showPref: false;
    }
  | {
      prefLabel: string;
      showPref?: true;
    }
) 
& (
    | { other: false; } 
    | { other: true; }
    );


const fn = (thing: Arg) => {};

fn({ showPref: false }); // <<< error on this line

fn({ showPref: false, other: true }); // <<< this is ok

🙁 Actual behavior

I see an error for fn({ showPref: false });:

Argument of type '{ showPref: false; }' is not assignable to parameter of type 'Arg'.
  Type '{ showPref: false; }' is not assignable to type '{ prefLabel: string; showPref?: true | undefined; } & { other: true; }'.
    Property 'prefLabel' is missing in type '{ showPref: false; }' but required in type '{ prefLabel: string; showPref?: true | undefined; }'.(2345)
input.tsx(7, 7): 'prefLabel' is declared here.

This is wrong because actually other is missing. The following line does not produce an error

🙂 Expected behavior

I would expect the error message to refer to the missing required property, not to an irrelevant union member

Contributor Guide