microsoft / microsoft/TypeScript
Misleading error message for missing properties in types composed of the intersection of certain multiple unions
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par la reproduction liée dans TypeScript Playground et comparez le diagnostic des deux appels à fn impliquant des intersections d’unions. Suivez la manière dont le compilateur sélectionne le membre d’union et la propriété manquante signalés ; le travail est terminé lorsque le premier appel signale la propriété manquante other plutôt que la propriété non pertinente prefLabel, tandis que le second reste valide.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100