Skip to content

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

Open

Description

@h-joo

🔎 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

Activity

  1. MartinJohns commented on Aug 20, 2023

    @MartinJohns
    Contributor

    Workaround: 0 as const.

  2. dragomirtitian commented on Sep 7, 2023

    @dragomirtitian
    Contributor

    Mateusz Burzyński (@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 ?

  3. Andarist commented on Sep 7, 2023

    @Andarist
    Contributor

    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

  4. dragomirtitian commented on Sep 7, 2023

    @dragomirtitian
    Contributor

    Mateusz Burzyński (@Andarist) Oh. Yeah. Forgot about that. Thanks 😅

  5. added
    Domain: Declaration Emit
    on Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: Declaration Emit

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions