microsoft / microsoft/TypeScript

Discriminated union not narrowing when mixing finite and infinite domain types in a single field

Aperta
#52,581 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experience Enhancement Help Wanted Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

discriminated union narrowing infinite finite domain

🕗 Version & Regression Information
  • This is the behavior in every version I tried (3.3.3, 4.9.4, and nightly), and I reviewed the FAQ for entries about narrowing and unions
⏯ Playground Link

Playground link with relevant code

💻 Code
export interface NoneMetric {
  readonly unit: "None";
  readonly value: number;
}

export type DurationMetric = {
  readonly unit: "Seconds";
};

export type ErrorMetric = {
  readonly unit: "Error";

  // Below types are finite; they prevent the `unit` field from discriminating the union in the `spec` object
  readonly value: 'fatal' | 'error' | 'warning';
  // readonly value: 0 | 1;
  // readonly value: boolean;
  // readonly value: null | undefined;

  // Below types are infinitely sized; these will give you the discriminating behavior you expect
  // readonly value: string;
  // readonly value: number;
  // readonly value: any;
  // readonly value: Array<0>;
};

export type MetricEvent = NoneMetric | DurationMetric | ErrorMetric;

const spec: MetricEvent = {
  unit: "Seconds",
  value: 10, // Expected an error here since `unit` should always narrow to `DurationMetric`
};
🙁 Actual behavior

When ErrorMetric.value is a finite domain type:
The spec object fully satisfies MetricValue (with no error) despite not satisfying any constituent members of that union. There is no error on spec.value being present despite unit being Seconds.

When ErrorMetric.value is an infinite domain type:
The spec object narrows to DurationMetric. There is an error on spec.valueindicating thatvaluedoes not exist onDurationMetric`.

🙂 Expected behavior

The spec object should always narrow to DurationMetric based on the discriminant unit field. The narrowing should not be impacted by the value field on other types since unit uniquely defines the discriminated union members.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e confronta i casi finiti e infiniti di ErrorMetric.value nell’unione MetricEvent. L’issue è risolta quando il discriminante unit restringe coerentemente l’oggetto a DurationMetric e rifiuta la proprietà value non valida.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.