timocov / dts-bundle-generator

A tool to generate a single bundle of dts with types tree-shaking
MIT License
762 stars 40 forks source link

export type * shouldn't export consts from the file #330

Closed UziTech closed 2 months ago

UziTech commented 2 months ago

Bug report

We are using dts-bundle-generator in marked and found that it is exporting some variables that aren't actually exported.

see https://github.com/markedjs/marked/issues/3426

Input code

https://github.com/markedjs/marked/blob/master/package.json#L90

Expected output

block and inline are not exported from 'marked'

Actual output

https://cdn.jsdelivr.net/npm/marked@14.0.0/lib/marked.d.ts

Additional context

It looks like it thinks because the variables are exported from an internal file that they should be exported by marked but they are not exported by marked.

UziTech commented 2 months ago

I tried adding --export-referenced-types=false and that didn't seem to do anything.

UziTech commented 2 months ago

looks like this is because we export the types from the file the consts are exported from

export type * from './rules.ts';

which only exports the types but dts-bundle-generator lists the consts as exported as well.

timocov commented 2 months ago

which only exports the types but dts-bundle-generator lists the consts as exported as well.

This is where I believe the misunderstanding is. export type was never meant to "export types only", it "exports everything but as types" https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export:

export type only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.

e.g. see here that an exported via export type statement const foo can be imported from another module without any issues, which means that it should be preserved in output.

timocov commented 2 months ago

But related to using export type and keeping it as "type", here is the issue https://github.com/timocov/dts-bundle-generator/issues/290 (basically at the moment the compiler doesn't allow to differentiate these cases thus it is not done).