-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
export = locale in index.d.ts doesnt allow synthetic import #2189
Comments
As a starter: this does not only affect the 'core' program of dayjs (as handled in your pr #2190), but any plugin and locale file. From your pr I suppose you are using dayjs in an esm environment. But dayjs is a commonjs library (the esm first implementation is still under construction; mostly called "dayjs 2.0", but according to the authors it will probably get 3.0). I made a pr for fixing all the (default) type definitions for esm (pr #1581) and the answer was "let's implement it in dayjs 2.0" (at least that is my interpretation 😄). The typescript handbook says that |
But it also break esm build, you can't Or maybe cjs and esm module should not use same export,
|
How about using Or In my (esm) application, I gave up to try to use 'dayjs/esm' because of the effects you see in your app too. |
TBH, This means it import all named exports from module But what we actually want to do is import the default export of module and if you are using esm,
So, for an esm TypeScript developer with eg: import dayjs from 'dayjs/esm';
export default dayjs; vendor/dayjs.d.ts
then you can |
You are right: It is quite a while since i wrote pr #1581, but as far as I remember the problems were:
So perhaps a (temporary) solution could be to use a copy of the type definitions from my pr in the local "typeRoots" of your program. |
I read your pr, the solution is very simple... there is no need move any type components defs, just add a /// <reference path="./locale/index.d.ts" />
import dayjs = require('dayjs')
export default dayjs; and rename all file extention in esm to esm build of dayjs are doing something like Unless someone didn't import And good news is But this will break users write code with https://github.com/search?q=dayjs%2Fesm%2Findex.js&type=code |
I think I still have my test project from the time of this pr. If I find it, I could try it in a commonjs environment (but not in the next 2 minutes 😄). |
On second thought I think it could be a fix... and it doesn't touch any code with commonjs. also you can't require esm pacakge in cjs, so it only need to work with esm. |
ASAIK type definitions are for typescript and typescript can be used for esm and commonjs environments. So we need type definitions for both systems and regarding Or did I miss something. |
because But that's not the point... |
Thanks for trying to figure out this historic problem. There are some problems like #277 and #313, which do not have a proper fix in the current project design. (At least to me) Most of them are related to Typescript and ESM, which is essential at present. Maybe a 2.0 version will be a better place to fix it and get rid of all the limitations on the current code. |
I think #2193 at least fix something, could you take a look? it doesn't touch cjs files, and only change some code generated in it doesn't fix the problme like |
As dayjs 2.0 is still a few months away, it is surely worth taking a deeper look at this lightweight extension of the dayjs esm architecture. I would appreciate this 👍 My pullrequests concerning dayjs 2.0 show that it is not so easy, to define an expandable architecture / plugin system in a typesafe environment. |
True, the plugin system introduced in dayjs is not really flowed the TypeScript way. That's maybe the main challenge in 2.0 version. |
Current interface merging is already the typescript way to support plugin IMO, it's what typescript do in their document. 🤔 https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation |
That is the easy part; problems arise, when you try to add a setter to a getter defined in core via interface merging. And you have to "preview" all the expansion points needed by plugins(even plugins that you don't know of 😄). |
Can I get an example? |
See pr #2128 (but it is a quite extensive pr; an implementation of the utc plugin for dayjs 2.0). In dayjs "core" we have a "getter" named The problem is not the js code (the way dayjs 1,x does it, will of course also run in dayjs 2.0 - but not in a typesafe way), I tried several ways to solve it, using my own ideas and any "trick" I found searching stackoverflow and other resources. But I could not define the overload signature in the utc module in a way that the compiler does not show an error and that the code does not throw during runtime. In the end I had to add the overload signature to the "core" module - but IMO a plugin should not require a modification of the module to be expanded (but it is hard to identify the "expansion" points beforehand). Any better solutions are welcome 😄 . |
Describe the bug
When I want to import dayjs as synthetic import in my lib, I get the following error :
export = dayjs;
~~~~~~~~~~~~~~~
This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag
Expected behavior
Application can build with this line : "import dayjs from 'dayjs';"
Information
The text was updated successfully, but these errors were encountered: