microsoft / microsoft/TypeScript
Union with non-distinct discriminating property breaks inference somewhat (confusing error messages, invalid intellisense)
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
TypeScript Version: 4.0.2
Search Terms: discriminated union, error messages
Code
type Action = {
type: 'action1';
payload: {
property1: string;
};
} | {
type: 'action1';
payload: {
property2: number;
};
} | {
type: 'action2';
payload: {
property3: boolean;
};
}
const action: Action = { // Actual error is here
type: 'action1',
payload: {
property3: true // Expected error here
}
}
Expected behavior:
Expected error at action.payload.property3 declaration, perhaps something like:
Object literal may only specify known properties, and 'property3' does not exist in type '{ property1: string; } | { property2: number; }'.
Also, once the object has been refined by the type action1, intellisense should only provide property1 and property2 as a suggestion when declaring the payload.
Actual behavior:
An error message at the first line of the declaration:
Type '{ type: "action1"; payload: { property3: true; }; }' is not assignable to type 'Action'.
Type '{ type: "action1"; payload: { property3: true; }; }' is not assignable to type '{ type: "action2"; payload: { property3: boolean; }; }'.o
Type '"action1"' is not assignable to type '"action2"'.(2322)
Also, once the object has been refined by the type action1, intellisense still provides property3 as a suggestion when declaring the payload.
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.
Évaluation
Cette issue n'a pas encore été évaluée.