Skip to content

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

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

Closed
rmunch opened this issue Dec 20, 2021 · 3 comments
Closed
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@rmunch
Copy link

rmunch commented Dec 20, 2021

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
@RyanCavanaugh
Copy link
Member

What is MyUnionType ?

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Jan 4, 2022
@rmunch
Copy link
Author

rmunch commented Jan 4, 2022

@RyanCavanaugh I got that typo fixed (MyUnionType should've been TypeFromLibA). Thank you!

@RyanCavanaugh
Copy link
Member

This should work fine in any package manager that sets up node_modules correctly (with 1.5 in the parent and 1.1 as a subfolder of B), which is now "all of them". If not, please open a new issue with a concrete repo we can clone and investigate. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants