-
-
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 module instead of namespace for TS declaration #751
base: dev
Are you sure you want to change the base?
Conversation
The source code uses modules, so now the types will too. This fixes the issue of not being able to use the default export although it exists.
Codecov Report
@@ Coverage Diff @@
## dev #751 +/- ##
===================================
Coverage 100% 100%
===================================
Files 154 154
Lines 1030 1030
Branches 162 162
===================================
Hits 1030 1030 Continue to review full report at Codecov.
|
Should this be a breaking change? It will change all the existing code from |
Yep it's a breaking type-checking change -- although I'd be surprised if anyone's code with To elaborate, TypeScript allows you to do There is no setting to allow you to do the opposite -- That's why I'd be surprised if anyone is actually able to use |
Hi! I am running into a similar issue with one of the plugins and I'm wondering if it's related. I am trying to add the utc plugin to my dayjs in typescript.
This fails with the error:
If I change to |
https://day.js.org/docs/en/installation/typescript And we will update this in the next major release. |
Yes, would be nice! Meanwhile we're forking/patching Dayjs to achieve that. Would be nice to have it by default. Thanks for the great library! |
is "remove "module" field as a work-around NaturalCycles/time-lib@d5f06ae" the key to this issue ? |
No, it's just a workaround. I think there are 2 issues to fix:
Right now I cannot say if this PR fixes both, or one, or none. But if you're positive about fixing this issue (even if it becomes a Breaking change for Typescript users) - I can try to put together my version of a fix as a separate PR. |
Related to #475 I believe |
Hey everyone, to add some more context for this type of change: The TypeScript definitions should reflect the actual JavaScript API. They should be the same; not different. That's what TS is designed for. One of the core issues is that dayjs is implemented with a monkeypatched, object-based API, but is exported as a default value in an ES-module. It's hard to write types for this in-between API, so there's no suprise there are issues from incompatibility. API object and monkey-patching: Line 31 in 14ab808
Lines 399 to 415 in 14ab808
And then exported as ES module default: Line 417 in 14ab808
The alternative is moving to typical ES module exports, without monkeypatching. This would play nicer with bundlers and tooling like TypeScript. Here is the above, mechanically converted: export const extend = (plugin, option) => {
plugin(option, Dayjs, dayjs)
return dayjs
}
export const locale = parseLocale
export const isDayjs = isDayjs
export const unix = timestamp => (
dayjs(timestamp * 1e3)
)
export const en = Ls[L]
export const Ls = Ls
export default dayjs In that case, the correct TypeScript definition is trivial. It's basically the same code, but without the implementations. Then both JS and TS would have the same ES module API that allows both styles: //
// Usage style - 1
//
import dayjs, { isDayjs } from 'dayjs'
// Now undefined, unless monkeypatched object is still used
dayjs.isDayjs
//
// Usage style - 2
//
import * as dayjs from 'dayjs'
// Defined, even without monkeypatched object
dayjs.isDayjs
// Error, not a function
dayjs()
// Not an error, is a function
dayjs.default() |
This would be the correct way to export this library. Typescript explicitly follows ES standards, and as such, the "ES Module" way of doing things is the "right" way. Check out the answer to this stack overflow question https://stackoverflow.com/questions/37565709/how-to-use-namespaces-with-import-in-typescript |
The source code uses modules, so now the types will too.
This fixes the issue of not being able to use the default export although it exists.
The existing workaround is to use a project-wide
tsconfig.json
which works around this issue (see below). It's less than ideal, as it affects the entire project of the user. This PR should resolve that.