Skip to content
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

Bad import from d.ts file was left inside declaration bundle #157

Open
guilhermetod opened this issue Nov 12, 2021 · 4 comments
Open

Bad import from d.ts file was left inside declaration bundle #157

guilhermetod opened this issue Nov 12, 2021 · 4 comments
Labels
Need More Information Further information is requested Needs reproduction A link to a reproduction is missing

Comments

@guilhermetod
Copy link

  • Version:
  • Rollup Version: 2.59.0
  • Operating System and version (if applicable): Ubuntu 20.04 (on Windows 11 WSL)
  • Node Version (if applicable): 16

Hey there, first, thanks for this library and the support on here. I tried to track down the cause of these issue, but I honestly could not find it. It seems that internal declaration files are not bundled and instead are referenced in an import statement with the original filepath, whch does not exist after build.

Reproduction

Expected Behavior

The content of src/my-interface.d.ts gets bundled into index.d.ts

interface MyInterface {
    shouldBeBundled: boolean;
}
declare function getData(): MyInterface;
export { getData };

Actual Behavior

A import to a non-existing file is created instead

import { MyInterface } from "./my-interface";
declare function getData(): MyInterface;
export { getData };

Changing the filename from my-interface.d.ts to my-interface.ts outputs the expected behavior.

@wessberg wessberg added Need More Information Further information is requested Needs reproduction A link to a reproduction is missing labels Nov 17, 2021
@wessberg
Copy link
Owner

wessberg commented Nov 17, 2021

Hey there. I'd like to understand a little more about the structure of your project, as this is not enough for me to go on to reproduce the issue.

But if your project structure looks like this:

- my-interface.d.ts
- index.ts

And index.ts relies on types declared inside my-interface.d.ts that isn't directly referenced with an import or triple-slash directive inside index.ts and looks something like this:

export function getData(): MyInterface {
    // ...
}

Then generating declarations with tsc would produce:

export declare function getData(): MyInterface;

Which is syntactically valid, but semantically an error, since MyInterface is an undeclared type.
You would experience the same with rollup-plugin-ts which generates the following declaration file:

declare function getData(): MyInterface;
export { getData };

In both cases, you should avoid referencing types that you have declared inside ambient contexts (.d.ts) files in user-facing declarations, since these types won't be exposed in your library. Instead, you should declare them inside TypeScript modules (.ts) files, and import them across the files in which you need them.

All of these things said, I'd love to see why an import declaration was left in your bundle, as that is definitely a bug that I'd like to investigate and fix. So if you could provide a minimal repro that causes this behavior, I'll look into it and set up a test case for it.

@wessberg wessberg changed the title Declaration files do not get bundled Bad import from d.ts file was left inside declaration bundle Nov 17, 2021
@wessberg
Copy link
Owner

Hi @guilhermetod,

Have you had a chance to look at my reply yet? As I wrote:

All of these things said, I'd love to see why an import declaration was left in your bundle, as that is definitely a bug that I'd like to investigate and fix. So if you could provide a minimal repro that causes this behavior, I'll look into it and set up a test case for it.

@guilhermetod
Copy link
Author

Hi @wessberg sorry for the wait. I only now realized I forgot to link the repo I had created to illustrate the issue.

In fact, I actually do reference MyInterface through an import, but it's almost like if that import got stripped during compilation.

I agree that using .ts instead of .d.ts is a very simple workaround, is just something I got used doing for 1) restricting the content of the file to only declarations; 2) avoiding generating empty files when I used the direct TypeScript compiler (changing my-interface.d.ts to my-interface.ts and running npx tsc --outDir dist will generate and empty module in dist/my-interface.js

@wagenet
Copy link

wagenet commented Mar 4, 2022

I'm having a similar issue. The problem is that I have .js with corresponding .d.ts files. The .js gets rolled up into the .ts but the .d.ts doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Need More Information Further information is requested Needs reproduction A link to a reproduction is missing
Projects
None yet
Development

No branches or pull requests

3 participants