Skip to content

type gets widened on declaration emit when inferred in destructuring context #55439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
h-joo opened this issue Aug 20, 2023 · 4 comments
Open
Labels
Bug A bug in TypeScript
Milestone

Comments

@h-joo
Copy link
Contributor

h-joo commented Aug 20, 2023

πŸ”Ž 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

@MartinJohns
Copy link
Contributor

Workaround: 0 as const.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Aug 24, 2023
@andrewbranch andrewbranch added this to the Backlog milestone Aug 24, 2023
@dragomirtitian
Copy link
Contributor

@Andarist I found a very similar issue. In the code below x is typed in ts as 0 | 1, but is typed as number in declarations.

export const x = Math.random() ? 1 : 0 // 0 | 1

Playground

Will your fix work for this example as well ?

@Andarist
Copy link
Contributor

Andarist commented Sep 7, 2023

Yes, I think so - what Jake commented on here is basically the same as your case above: https://github.com/microsoft/TypeScript/pull/55445/files#r1300377161

@dragomirtitian
Copy link
Contributor

@Andarist Oh. Yeah. Forgot about that. Thanks πŸ˜…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants