rollup-plugin-dts icon indicating copy to clipboard operation
rollup-plugin-dts copied to clipboard

How can I export all types in the generated index.d.ts file?

Open LOVETOO opened this issue 1 year ago • 8 comments

By default, in the generated .d.ts file, only the type of the entry file is exported; but when other projects use this npm package, they need to use more types, and those types are not exported.

Version number: rollup@4 typescript@5 rollup-plugin-dts@6

LOVETOO avatar Oct 29 '24 13:10 LOVETOO

Same question

az-oolloow avatar Dec 04 '24 03:12 az-oolloow

I ran into the same problem and did not find a satisfying solution: https://github.com/utopia-os/utopia-ui/pull/122#issuecomment-2665845557

ulfgebhardt avatar Feb 18 '25 14:02 ulfgebhardt

Please provide a minimal reproduction 🙏🏻.

Maybe you don't export thoes types. This plugin works like tree-shaking in ES modules, removing types that are not exported.

kricsleo avatar Feb 19 '25 10:02 kricsleo

Please provide a minimal reproduction 🙏🏻.

Maybe you don't export thoes types. This plugin works like tree-shaking in ES modules, removing types that are not exported.

The types are part of the resulting .d.ts, but they are not exported there. This does not allow to import them from a third party.

This happens with nested types e.g.:

export interface Mother {
  element: Child
}

The Child is not explicitly exported, but is inherently needed for the Interface.

It would be good if we would have an option to export such implicit types without the need to explicitly export them.


A simple in-practice example is an Interface you expose like this:

interface SomeType {}

export function do(input: SomeType){
  ...
  return
}

The resulting .d.ts looks like this:

interface SomeType {}

declare function do(input: SomeType): void;

export { do };

Now I want to call the do() function from an external library. It cannot excess SomeType since its not explicitly exported. The type is present in the .d.ts, but i cannot import it.

Current workaround: Export the type and all subtypes explicitly

export interface SomeType {}

export function do(input: SomeType){
  ...
  return
}

The resulting .d.ts looks like this:

interface SomeType {}

declare function do(input: SomeType): void;

export { type SomeType, do };

ulfgebhardt avatar Feb 19 '25 16:02 ulfgebhardt

I believe that's the expected and standard behavior: users can only import what you explicitly export, correct? 🤔

It's like defining two functions:

function private() {
  ...
}
function public() {
  private();
  ...
}
export { public };

Users can only access the explicitly exported public function. The types work the same way, it shouldn't change the intent of your source code.

kricsleo avatar Feb 20 '25 04:02 kricsleo

I agree - but this is very tedious to maintain.

It would be good to have the option to export all defined type used in public interfaces.

ulfgebhardt avatar Feb 20 '25 10:02 ulfgebhardt

Implicitly adding "export" may cause export duplicate name conflicts.

type Child = string

export interface Public {
  child: Child
}

type PublicChild = number

export { PublicChild as Child }

// Look, there are now two "Child" exports.
export type Child = string

export interface Public {
  child: Child
}

type PublicChild = number

export { PublicChild as Child }

kricsleo avatar Feb 25 '25 12:02 kricsleo

I see. So there is no chance to get an option to try to export all types, since it has structural weaknesses?

We need to export all types and subtypes explicitly in consequence.

Its fine with me - maybe this behaviour and the resulting limits could be documented to prevent further questions.

Thank you for your time to investigate!

ulfgebhardt avatar Feb 25 '25 13:02 ulfgebhardt