microsoft / microsoft/TypeScript

type gets widened on declaration emit when inferred in destructuring context

Open
#55,439 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Declaration Emit
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

declaration, widened, d.ts, destructuring

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries
⏯ Playground Link

https://www.typescriptlang.org/play?jsx=0&module=1&ts=5.1.6#code/GYVwdgxgLglg9mABMOcAUBKAXIg3gTxwEYBfPAKESsQCcBTKEGpXRQxIxE878ugDwAOcGlEQQEAZzGt8iALyIADFwXJUmANxA

💻 Code
function foo(): {y: 1} {
    return { y: 1 }
}

export const { y = 0 } = foo();
🙁 Actual behavior

y is number in d.ts files, although the language service says y is 1|0

🙂 Expected behavior

y is 1|0 in d.ts files.

Additional information about the issue

Without the = 0 assignment y properly becomes type 1 in d.ts

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

Use the linked TypeScript Playground and the provided destructuring example to reproduce the declaration emit. Compare the generated d.ts type for y with the language service result, then verify that the emitted type remains 1|0 when the default assignment is present while the no-default case remains 1.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.