-
Notifications
You must be signed in to change notification settings - Fork 42
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
Using 'seperate' declaration with CommonJS creates invalid types #327
Comments
Yeah, don't use
|
Another issue somewhat related to this, is that the type definitions that are emitted for CommonJS are not really correct. For example, given the aforementioned input the following type is generated: export declare const message = "Hello World!"; This is of course correct for the ESM bundle, but for CommonJS I would expect something like the following: declare const message = "Hello World!";
export = { message }; |
@jonkoops that's the typescript compiler giving that output. I don't believe there's a way to get it to do that, but let me know if you know a way. I believe that output should work fine though. |
Yeah, I think the only way to do so would be to convert the source code to CommonJS as well. There really is no way for the TypeScript compiler to emit valid CommonJS types from an ESM source, as far as I know. Perhaps the best solution here is to simply not link any types to the CommonJS input, which I believe is the default behavior with |
Btw. esm.sh doesn't seem to detect types when emitted with "inline", but does with "separate". (yes, I'm publishing a deno module to npm and load it back into deno via esm.sh) |
If the package works with vanilla tsc then I'd recommend opening an issue on esm.sh's repo. |
Description
When building an NPM library that supports both ESM and CommonJS, and has the types separated, an invalid
exports
is generated:Here, both of the
types
fields refer to the same file, but according to the TypeScript Handbook this is not allowed:Steps to reproduce
Create a file called
test.ts
and add some dummy content with an export:Create a build script under
scripts/build_npm.ts
with the following contents:Then, build the NPM package with the following command:
Once the package has been built, it can be inspected using the Are the types wrong? CLI:
Which should show the following error:
Solution
As per the suggestion in the TypeScript Handbook, a second file should be added specifically for the CommonJS version and linked from the
types
field.The text was updated successfully, but these errors were encountered: