microsoft / microsoft/TypeScript

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

Open
#52,581 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare the finite and infinite ErrorMetric.value cases in the MetricEvent union. The issue is resolved when the unit discriminant consistently narrows the object to DurationMetric and rejects the invalid value property.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.