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

[Bug] Typescript Declaration file error #1528

Open
Mister-Hope opened this issue Jun 8, 2021 · 15 comments
Open

[Bug] Typescript Declaration file error #1528

Mister-Hope opened this issue Jun 8, 2021 · 15 comments

Comments

@Mister-Hope
Copy link

Mister-Hope commented Jun 8, 2021

Please convert dayjs/esm/index.d.ts to export default dayjs

image
image

Declaration file is using export = which is same to exports in cjs, while the actural code is export default which is exports.default in cjs.

This makes error when the env not allow users to open allowSyntheticDefaultImports and esModuleInterop

@Mister-Hope Mister-Hope changed the title Typescript Declaration file error [Bug] Typescript Declaration file error Jun 8, 2021
@iamkun
Copy link
Owner

iamkun commented Jun 9, 2021

this will introduce a new problem, please refer to #1281

@Mister-Hope
Copy link
Author

What is the problem ? I read through #1281 and did not found what you mean.

You have already deleted module entry, so align esm/index.d.ts with esm/index.js should be the expect behavior.

Please list one of the problems, I am not catching you and I can not think out one.

@Mister-Hope
Copy link
Author

users must explicit use dayjs/esm in es module env now if they want.

And they will read dayjs/esm/index.d.ts when using typescript only by doing import dayjs from 'dayjs/esm', so why should typescript decalre a module.exports intead of a export default?

What are the acual problem?

@BePo65
Copy link
Contributor

BePo65 commented Jun 12, 2021

I have the same problem:
I use import dayjs from 'dayjs/esm'; and get a warning saying:

index.d.ts(3, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.

And that is a correct warning, as dayjs/esm/index.d.ts is identical to dayjs/index.d.ts. That means that dayjs/esm/index.d.ts does not reflect the change in dayjs/esm/index.ts line 433 that says:

export default dayjs;

So I agree with @Mister-Hope that dayjs/esm/index.d.ts needs a change in line 3 with the new content being:

export default dayjs;

or perhaps I am missing an important side effect of such a change :-)

@Mister-Hope
Copy link
Author

Mister-Hope commented Jun 12, 2021

Nope probably @iamkun isn't expert at ts. And since he used module field to trigger a side effect, he thinks all have a side effect 🤔

@BePo65
Copy link
Contributor

BePo65 commented Jun 12, 2021

BTW I ran into the same problem when trying to import the customParseFormat plugin: in the index.js it says export default (function (o, C, d)...., but the corresponding index.d.ts says export = plugin.

@Mister-Hope
Copy link
Author

BTW I ran into the same problem when trying to import the customParseFormat plugin: in the index.js it says export default (function (o, C, d)...., but the corresponding index.d.ts says export = plugin.

Actually all the declaration files in esm needs to be corrected

@BePo65
Copy link
Contributor

BePo65 commented Jul 8, 2021

One more point: if dayjs/esm should be a 'esm' version of the package (which in my opinion it definitely should), then of course it will need a package.json of itself with a "type": "module" in it.

Otherwise node will look up the tree for the next package.json and will find that of dayjs. And of course here in the "root" path we must not have a "type": "module" or we will break all existing codes (see the lengthy discussion in issue #1281.

My only problem is that if we want to use the default export plus the destructuring imports at the same time we will have to redefine all exports in the .d.ts files (at least I didn't find a better way). I really don't like code duplication, but probably we will have to go this way. I'm still working of a PR to this.

@Mister-Hope
Copy link
Author

Mister-Hope commented Jul 8, 2021

One more point: if dayjs/esm should be a 'esm' version of the package (which in my opinion it definitely should), then of course it will need a package.json of itself with a "type": "module" in it.

This will have sife effects in some situation.

If a code wants to work both in cjs and esm when just exporting a function, it should use named export intead of default export.

And this is the issue ikun run into.

Now you should import dayjs/esm to avoid problems. And I don't think a new package like dayjs-esm should be published.

Ikun will probably need to change to named exports and raise to v2 to include them in one package.

@BePo65
Copy link
Contributor

BePo65 commented Jul 8, 2021

Just to understand the problem I created demo projects for all cases:

  1. node using const dayjs = require('dayjs') and with just a plain package.json => everything runs as expected
  2. node using import dayjs from 'dayjs' and with "type": "module" in the package.json => everything runs as expected
  3. typescript using import dayjs from 'dayjs' and with just a plain package.json and with "compilerOptions": {"allowSyntheticDefaultImports": true, "esModuleInterop": true} in the tsconfig.json => everything runs as expected, as in my understanding this boils down to using the library as umd-/commonjs packages

Now IMHO @iamkun made the clever decision to integrate dayjs as esm package in the generated package in form of a subfolder called dayjs/esm.

So in my program (that is using the dayjs package) if and only if I want to use dayjs as an esm module, I would use dayjs/esm instead of dayjs without changing any other code.

If I do this with dayjs/esm just out of the box, I get the effect that node complains about a missing "type": "module" (as the next package.json up the line does not contain such an entry - and should not, as this would kill the usage of import from 'dayjs').

  1. Therefore I added a package.json with "type": "module" into the folder dayjs/esm and without "compilerOptions": {"allowSyntheticDefaultImports": true, "esModuleInterop": true} in the tsconfig.json (ok and I fixed the type definition files to match the export default of the *.js files and have named exports too) and now this runs like a charm.

This way we could have both worlds without using 2 packages (dayjs and e.g. dayjs-esm) and without the problems described in issue #1281 (because like you all confirmed, there is no simple way to configure package.json of a library so that it is possible to use this library as umd/commonjs and esm - at least not in an node environment).

Using 2 different packages is something we should avoid, as this makes development of this great package not really easier - I suppose.

And to the question of semantic versioning: is this really a major (i.e. breaking) change? We only change the type definitions and at least I could not get the current type definitions into compiling without error - so does anyone else?

Or does my "great" solution (just joking :-) miss some side effects that would kill any existing application using dayjs? At the moment all existing solutions should be using the path dayjs, shouldn't they)?

@BePo65
Copy link
Contributor

BePo65 commented Jul 17, 2021

I addressed these points in pr #1581

@Mister-Hope
Copy link
Author

Any update of this?

1 similar comment
@Mister-Hope
Copy link
Author

Any update of this?

@naveedahmed1
Copy link

any update?

@hakimio
Copy link

hakimio commented Nov 14, 2023

There hasn't been any updates to v2 (alpha) git branch since 09/2022, so no, there doesn't seem to be anything done to improve the ESM/TS situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants