Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
812 stars 70 forks source link

Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier. #178

Closed ghostdevv closed 2 years ago

ghostdevv commented 3 years ago

Checklist

I am running this on my code and it works fine, well I am using http://npmjs.com/tsup to bundle my typescript code for esm and cjs platforms.

I ran into a issue with it not generating declare on enums when used a certain way, I created a repro here: https://github.com/ghostdevv/dts-plugin-enum-repo

I know it doesn't ticket checkbox 2 but I think this is within the scope of a bug

ghostdevv commented 2 years ago

bump

Swatinem commented 2 years ago

I think the problem comes from your handcrafted .d.ts file:

https://github.com/ghostdevv/dts-plugin-enum-repo/blob/7e03e0a291cc7f81c4cf1a288fbbf32d67858980/src/test.d.ts#L1

It indeed is missing a declare modifier.

ghostdevv commented 2 years ago

@Swatinem I tested this and with exporting a enum with:

export declare enum TestEnum {
    A,
    B,
    C,
    D
}

it doesn't work

everlose commented 2 years ago

the same。tsc generate file test.d.ts

export declare type PushEvent = {...}

but,dts plugin roll the definition index.d.ts

declare type PushEvent = {...}

Lost export,my user cannot import this definition

Swatinem commented 2 years ago

@everlose Well are you exporting it again from the index.d.ts?

ashuvssut commented 2 years ago

the same。tsc generate file test.d.ts

export declare type PushEvent = {...}

but,dts plugin roll the definition index.d.ts

declare type PushEvent = {...}

Lost export,my user cannot import this definition

I have this issue too.

@everlose Well are you exporting it again from the index.d.ts?

@Swatinem How to export from index.d.ts? index.d.ts is generated by this plugin. these decalations types were exported from test.d.ts but when it got bundled by this plugin into a single index.d.ts , that export keyword is lost in that index.d.ts

My user now cannot import those definition because the export keyword is lost in index.d.ts

Swatinem commented 2 years ago

I mean obviously if you want something be exported from index.d.ts that originally lives in test.d.ts you gotta export { PushEvent } from "./test"; or something similar.

ashuvssut commented 2 years ago

Hi @Swatinem . Thankyou for replying

Let me explain you through an example. I have setup a test repo to show you the exact problem.

You can clone and test. Expand to see Commands to setup && run the example
git clone https://github.com/ashuvssut/rollup-ts-test && cd rollup-ts-test && yarn && yarn dev

So what happened? After we did rollup -c, TS compiler will generate the declatation .d.ts files in the /@types (in tsconfig: declarationDir: "/@types") so you will see something like this:-

@types/
    |---- index.d.ts
    |---- types.d.ts
dist/
tsconfig.json
rollup.config.js
.
.
.
.

IMPORTANT Observe that index.d.ts & types.d.ts have export keywords that you were asking about

So, that index.d.ts is next fed to to rollup-plugin-ts (see rollup.config.js). this will result in bundling all @types/*.d.ts file to dist/index.d.ts

Now Observe the problem in dist/index.d.ts. We lost the export keywords that were present in d.ts files in @types/

Hope this helps. Thanks

Swatinem commented 2 years ago

Yes, the only thing you are exporting from index.ts is this:

export { THEMES };

https://github.com/ashuvssut/rollup-ts-test/blob/f12cf5e62182d0bf2d0aafd405029411e0b9cf62/src/index.ts#L12

The other things are not being exported because you didn’t explicitly tell them to be. You can still refer to the other things indirectly, via typeof THEMES, if you absolutely want to. Otherwise you need to specifically re-export the stuff.

ashuvssut commented 2 years ago

@Swatinem yes thankyou. It makes sense now

I gave the plugin input option as input: "./@types/index.d.ts", and that's why only exported members of index.d.ts had the export keyword. As you suggested we have to re-export through index.ts. I followed that and it worked

But now suppose there are 100+ such types from different different types.ts files... now I have to re-export all those 100+ types through index.ts.😫 I wish there was a easy way to do that EDIT: We can use JS Barreling technique to re-export at once.

Anyways thankyou very much for bearing with my silly comments and questions. I am just a TS newbie studying in college😅

ghostdevv commented 2 years ago

I would like to reclarify the original purpose of this issue. When using a enum in a ts file the built dts does not have a enum prefixed with the declare or export modifier. Which is invalid. I have the repro here and you can see the source and build result.

https://github.com/ghostdevv/dts-plugin-enum-repo

Swatinem commented 2 years ago

Your repro has a index.ts that does a export * from './test';, only that you have both a test.ts and test.d.ts file. It looks like the typescript resolution prefers the .d.ts file, which is missing the declare keyword.

It would be nice to resolve that ambiguity first, and create a repro that starts with valid .d.ts files. The .ts mode is a maintenance nightmare.

ghostdevv commented 2 years ago

Your repro has a index.ts that does a export * from './test';, only that you have both a test.ts and test.d.ts file. It looks like the typescript resolution prefers the .d.ts file, which is missing the declare keyword.

it is my understanding and assumption from extensive use of enums that you do not require the declare keyword when you are exporting an enum.

It would be nice to resolve that ambiguity first, and create a repro that starts with valid .d.ts files. The .ts mode is a maintenance nightmare.

You are right about this


I will close this issue as I am unable to re-reproduce this! thank you for your time

hushaudio commented 2 years ago

Your repro has a index.ts that does a export * from './test';, only that you have both a test.ts and test.d.ts file. It looks like the typescript resolution prefers the .d.ts file, which is missing the declare keyword.

it is my understanding and assumption from extensive use of enums that you do not require the declare keyword when you are exporting an enum.

It would be nice to resolve that ambiguity first, and create a repro that starts with valid .d.ts files. The .ts mode is a maintenance nightmare.

You are right about this

I will close this issue as I am unable to re-reproduce this! thank you for your time

Randomly found this looking for a typescript solution

declaring the enum as an export did it for me. Appreciate the tip 👍 export enum MyEnum { PROP1, PROP2 };