Comments (13)
Thanks for the detailed bug report. Yes, work in progress, just a little slow over the holiday period. I'll keep you posted.
from date-fns-tz.
Checked it out in my full project and seems good. Thank you! π
from date-fns-tz.
I have implemented @kamranayub 's suggestion in 1.0.7-beta.1
. If you would try it out in your respective projects in the coming days and let me know if it is compatible with all relevant versions of TS that would be appreciated. Then I'll do a full release.
from date-fns-tz.
This is being fixed in https://github.com/marnusw/date-fns-tz/commits/fixTypescript, but it is not ready yet. Tried npm install --save marnusw/date-fns-tz#fixTypescript
but it didn't work.
Also tried cloning this repo, switching to the fixTypescript branch and running the following:
npm install
chmod +x scripts/build/*
npm run build
export PACKAGE_OUTPUT_PATH=./path/to/your/project/node_modules/date-fns-tz
npm run package
... but encountered multiple issues during build and packaging and compiling, so I assume the branch reflects work in progress.
from date-fns-tz.
I have published 1.0.0-beta.1
where the reported issue appears to be fixed. Using
import utcToZonedTime from 'date-fns-tz/utcToZonedTime'
works correctly now, but the named import
import { zonedTimeToUtc } from 'date-fns-tz'
errors with Type error: Module '"date-fns-tz"' has no exported member 'zonedTimeToUtc'. TS2305
. I've not worked with Typescript all that much so I'm wondering whether either of you have some insight into why this happens? I've compared the typings file with the one used by date-fns
and I can't spot any issues. If you don't know I might ask the date-fns
maintainers if they would mind helping out.
from date-fns-tz.
Thanks for the update @marnusw!
I looked at the issue you're talking about, and found an explanation and a potential fix here.
I tried changing the locale?: Locale
line to locale?: import('date-fns').Locale
, and removed the import
at the top, and that seems to have settled it down for me.
Unfortunately I'm not well-versed in creating declarations files either, so I'm not sure if there's a better way to fix it. It seems like this could get clunky if you needed a type from date-fns in multiple places throughout the declaration file...
from date-fns-tz.
Thank you @klanchman!
It looks like all Typescript issues have been resolved in 1.0.0
. If so this issue can be closed.
from date-fns-tz.
@klanchman It appears this import('date-fns').Locale
is not supported in TS 2.8.3 (which is the create-react-app-typescript fork version). I can explore what typings might be backwards compatible to submit a PR potentially.
edit: Here's an example typings file that works fine in TS 2.8 and 3.2:
declare module 'date-fns-tz' {
import { Locale } from "date-fns"
export type OptionsWithTZ = {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
additionalDigits?: 0 | 1 | 2
timeZone?: string
locale?: Locale
includeSeconds?: boolean
addSuffix?: boolean
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'
roundingMethod?: 'floor' | 'ceil' | 'round'
awareOfUnicodeTokens?: boolean
}
export function toDate(
argument: Date | string | number,
options?: OptionsWithTZ
): Date
export function utcToZonedTime(
date: Date | string | number,
timeZone: string,
options?: OptionsWithTZ
): Date
export function zonedTimeToUtc(
date: Date | string | number,
timeZone: string,
options?: OptionsWithTZ
): Date
}
declare module 'date-fns-tz/toDate' {
import { toDate } from 'date-fns-tz'
export default toDate
}
declare module 'date-fns-tz/zonedTimeToUtc' {
import { zonedTimeToUtc } from 'date-fns-tz'
export default zonedTimeToUtc
}
declare module 'date-fns-tz/utcToZonedTime' {
import { utcToZonedTime } from 'date-fns-tz'
export default utcToZonedTime
}
from date-fns-tz.
Ah, I didn't notice that. I'm also using the create-react-app-typescript fork, but with TS 3.1.
Your example deviates quite a bit from how the definition file is set up currently. It also doesn't account for the fp
, esm
, and esm/fp
modules, all of which also use OptionsWithTZ
. The way it's structured now lines up pretty closely with how date-fns
is doing it too, which I imagine was the intent.
That said, it might not be a problem to take your example and just duplicate the OptionsWithTZ
definition across each module. I haven't looked closely at how the typings are being generated to know for sure. @marnusw would know more about that π
from date-fns-tz.
This should be simple enough to change. I'll take a look when I can. Thank you for investigating.
from date-fns-tz.
@kamranayub when I change the typings file to
declare module 'date-fns-tz' {
import { Locale } from "date-fns"
export type OptionsWithTZ = {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7
additionalDigits?: 0 | 1 | 2
timeZone?: string
locale?: Locale
includeSeconds?: boolean
addSuffix?: boolean
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'
roundingMethod?: 'floor' | 'ceil' | 'round'
awareOfUnicodeTokens?: boolean
}
}
Then the other module declarations lower down break because OptionsWithTZ
is no longer globally defined. It looks like this can be fixed by adding import { OptionsWithTZ } from "date-fns-tz"
to each of the other module declaration blocks.
But what about the final section that defines // dateFns Global Interface
with interface dateFns { ... }
. I'm not familiar enough with TypeScript to know how important that section is or how it will be used. If it's not important it can be removed and we should have a working solution, if not, we need to find a way to make OptionsWithTZ
available to that interface definition which is also compatible with all the relevant TS versions. Any thoughts?
from date-fns-tz.
Looks to be working for me with TypeScript 3.1 and 3.3 π
from date-fns-tz.
Released in 1.0.7
.
from date-fns-tz.
Related Issues (20)
- `utcToZonedTime` should not be doing any conversion when `"UTC"` is passed in as the time zone HOT 1
- `utcToZonedTime` outputs a date in locale time
- formatInTimeZone considers DTS even on UTC when the env TZ has DTS HOT 1
- Not sure how to reason about this library when the underlying Unix timestamp gets modified
- Wrong parsed date with time at 24:00:00
- getTimezoneOffset for UTC+[X] does not work
- When input not ISO8601 time string like 2023/11/11 to toDate will return wrong date
- `formatInTimeZone(d, tz)` shows the wrong time for one hour if the system zone has DST, even though `tz` doesnβt
- Set default timezone HOT 1
- Support for date-fns v3 HOT 52
- Only named exports may use 'export type' HOT 2
- `RangeError: Invalid time zone specified:` error caused by usage of `formatInTimeZone` function for some folks HOT 5
- Upgrade from date-fns 2.30 to 3.0.6: Cannot find module 'date-fns/format/index.js' from 'node_modules/date-fns-tz/format/index.js' HOT 5
- Unable to resolve path to module 'date-fns-tz' HOT 11
- Improve Documentation HOT 3
- RangeError: Invalid time value - formatInTimeZone() Timezone offset value returning error. How to use this function with offset?
- v3 in Readme but latest on NPM still v2? HOT 4
- `typings.d.ts` no longer accurate for named exports in V3 HOT 4
- `formatInTimeZone` does not use the `timezone` in `options` to parse the input `date` string HOT 1
- getTimezoneOffset second argument is required HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date-fns-tz.