microsoft / microsoft/TypeScript

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

Open
#63,050 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Error Messages Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

double error, same message twice, ambient declaration, string literal union

### 🕗 Version & Regression Information

This changed between versions 3.9.7 and 4.0.5

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/CYUwxgNghgTiAEYD2A7AzgF3gDwFzwCIoD4AfQgIwIChrl0sBPfFAVwFsKQZ4BeHIA

### 💻 Code

```ts
declare const x: "a" | "b"

const y: number = x
// ~
// Type 'string' is not assignable to type 'number'.
// Type 'string' is not assignable to type 'number'. (2322)
```

### 🙁 Actual behavior

You see same error message twice

### 🙂 Expected behavior

The error message should be the one in 3.9.7 ie...
```
Type '"a" | "b"' is not assignable to type 'number'.
Type '"a"' is not assignable to type 'number'. (2322)
```

Or the following message just once also works...
```
Type 'string' is not assignable to type 'number'. (2322)
```

### Additional information about the issue

_No response_

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 provided TypeScript Playground reproduction and compare its diagnostic with the stated 3.9.7 and 4.0.5 behavior. Trace the compiler's type-checking diagnostic path for assigning the ambient string-literal union to number; done means the duplicate message is removed and the diagnostic matches one of the expected forms.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.