Skip to content

Comments

Fix duplicate error message for literal union types#63180

Open
erdinccurebal wants to merge 1 commit intomicrosoft:mainfrom
erdinccurebal:fix/no-duplicate-error-literal-union
Open

Fix duplicate error message for literal union types#63180
erdinccurebal wants to merge 1 commit intomicrosoft:mainfrom
erdinccurebal:fix/no-duplicate-error-literal-union

Conversation

@erdinccurebal
Copy link

Fixes #63050

Problem

When assigning a non-enum union of string/number/bigint literals to an incompatible type, the error message would display the same information twice. For example:

declare const x: "a" | "b"
const y: number = x

Previously produced:

Type 'string' is not assignable to type 'number'.
  Type 'string' is not assignable to type 'number'.

The union "a" | "b" was generalized to string, which is identical to the per-member elaboration (each member also generalizes to string), resulting in a duplicate message.

Fix

In reportRelationError, after computing the generalized base type, check whether the source is a non-enum union whose members all share the same base type (i.e., the generalized form is a single non-union type). In that case, skip the generalization for display purposes so the error shows the original literal union instead:

Type '"a" | "b"' is not assignable to type 'number'.
  Type 'string' is not assignable to type 'number'.

The two lines now provide complementary information: the first shows what the type is, the second shows why it's incompatible.

Unions with mixed base types (e.g. 1 | ""number | string) are still generalized as before, since the generalized form differs from any single member's elaboration.

Baseline changes

Three existing baselines are updated — all had duplicate messages that are now fixed:

  • typeOfOperator1: typeof x result union was generalized to string, duplicating the elaboration
  • bigintPropertyName: 6n | 7n | 8n (type alias Q) was generalized to bigint, duplicating the elaboration
  • complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound: "text" | "email" was generalized to string, duplicating the elaboration

When a non-enum union of literals (e.g. "a" | "b") is assigned to an
incompatible type, the error would generalize the union to its base
type (e.g. string), producing a duplicate of the per-member elaboration
message. Now, when the generalized form collapses to a single non-union
base type, the original literal union is shown instead.
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Feb 22, 2026
@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Feb 22, 2026
@erdinccurebal
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Double error message when referencing variable of type union of string literals from ambient declaration

2 participants