Skip to content

Union type imported from external library collapsed in emitted type declarations #47205

Closed
@rmunch

Description

@rmunch

Bug Report

I've got an application that consumes two libraries like so:
Screen Shot 2021-12-20 at 12 54 18 PM

The problem I'm facing is that the TypeScript build for LibB is outputting a collapsed "snapshot" of a union type that is imported from LibA rather than preserving a reference to the externally-defined union type. Because of this, when the application is using a newer version of LibA, the application cannot use types from the union that do not exist in the older version referenced by LibB.

As a workaround, I can update LibB to use the newer version of LibA, publish a new version, and update App to point to it, but since the implementation of LibB does not need to be updated, this step should be unnecessary.

I believe there have been various past proposals to solve this (such as in #35654), as well as a workarounds for specific use cases, but I haven't found a workaround that solves this particular case.

Thank you!

🔎 Search Terms

preserve type alias
prevent union type collapse

🕗 Version & Regression Information

v4.5.4, and other recent versions.

⏯ Playground Link

Because this issue only occurs when a project uses the emitted declarations from another project, I may not be able to provide a playground link.

💻 Code

# LibA (as of v1.1.0)
export type TypeFromLibA = "a" | "b";

# LibA (as of v1.5.0)
export type TypeFromLibA = "a" | "b" | "c";
# LibB
import type TypeFromLibA from "lib-a"; // ex. as npm library

export const functionFromLibB = (val: TypeFromLibA) => {
   ...
}

🙁 Actual behavior

# App

import functionFromLibB from "lib-b";

functionFromLibB("a"); // OK
functionFromLibB("c"); // Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.ts(2345)

🙂 Expected behavior

# App

import myFunction from "lib-b";

myFunction("a"); // OK
myFunction("c"); // OK

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions