Microsoft/TypeScript

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

Open

#52,581 opened on Feb 2, 2023

View on GitHub
 (5 comments) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
Experience EnhancementHelp WantedSuggestion

Description

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.

Contributor guide