marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.05k stars 114 forks source link

Flow: Namespace '"date-fns"' has no exported member 'Locale' #12

Closed kjirou closed 4 years ago

kjirou commented 5 years ago

I compiled the following code...

src/index.ts)

import {format as dateFormat} from 'date-fns-tz';

dateFormat(new Date(), 'YYYYMMdd', {timeZone: 'America/New_York'});

The following error occurred.

./node_modules/.bin/tsc --project tsconfigs.json
node_modules/date-fns-tz/typings.d.ts:34:31 - error TS2694: Namespace '"date-fns"' has no exported member 'Locale'.

34   locale?: import('date-fns').Locale
                                 ~~~~~~

Found 1 error.

Environments

package-lock.json)

{
  "name": "reproduce-date-fns-tz-typing-bug",
  "version": "1.0.0",
  "lockfileVersion": 1,
  "requires": true,
  "dependencies": {
    "date-fns": {
      "version": "2.0.0-alpha.13",
      "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.0.0-alpha.13.tgz",
      "integrity": "sha1-oP1yPEZ7XFupi8nX/hvu9eak1ZY="
    },
    "date-fns-tz": {
      "version": "1.0.6",
      "resolved": "https://registry.npmjs.org/date-fns-tz/-/date-fns-tz-1.0.6.tgz",
      "integrity": "sha512-g0IY9CebRuyB5e2GIueO9mHpySpauMxSkbdkmVQEWi/R3w49ikYxC3dBNruGSOegwiNLI9cjuM0POZOFX0r8zQ=="
    },
    "typescript": {
      "version": "3.3.3",
      "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.3.tgz",
      "integrity": "sha512-Y21Xqe54TBVp+VDSNbuDYdGw0BpoR/Q6wo/+35M8PAU0vipahnyduJWirxxdxjsAkS7hue53x2zp8gz7F05u0A=="
    }
  }
}

tsconfigs.json)

{
  "compilerOptions": {
    "lib": [
      "es2018"
    ],
    "module": "commonjs",
    "newLine": "LF",
    "target": "es5",
    "strict": true
  },
  "include": [
    "./src"
  ]
}
marnusw commented 5 years ago

This library is an extension of date-fns@2.0.0-alpha and has date-fns specified as a peer dependency. The date-fns library contains the different locales and exports the Locale type definition. Please import it from there when necessary.

chasingmaxwell commented 5 years ago

I'm running into this as well when trying to upgrade from 0.1.1 to 1.0.7. I'm confused by your comment, @marnusw. The OP was not importing Locale, just format and dateFormat from date-fns-tz. This seems like an issue with the peer dependency range specified in date-fns-tz for date-fns, which is currently >=2.0.0-alpha.13. I'm using date-fns@2.0.0-alpha.37. Seems date-fns-tz is not actually compatible with the type definitions in that version of date-fns.

marnusw commented 5 years ago

The intention is not that date-fns-tz should re-export Locale. The types should match up so it can work together seamlessly with date-fns though. I, unfortunately, don't use this library in a statically typed project at the moment so I don't have full visibility of the current typings. I don't think much would have changed in the recent date-fns versions but it is possibly something fell out of sync.

Looking at the OP's question again I still can't tell that it is necessarily a problem with date-fns-tz. If you could provide some more info on exactly what isn't working for you and how you think this lib might be at fault I could investigate it. I'll reopen this issue in the meantime.

chasingmaxwell commented 5 years ago

date-fns-tz imports the type Locale from date-fns here: https://github.com/marnusw/date-fns-tz/blob/master/src/index.js.flow#L4

As far as I can tell looking at date-fns that type is not exported. So, when I run flow in a project depending on date-fns-tz, I get the following error:

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ node_modules/date-fns-tz/index.js.flow:4:15

Cannot import Locale because there is no Locale export in date-fns.

     1│ // @flow
     2│ // This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.
     3│
     4│ import type { Locale } from 'date-fns'
     5│
     6│ type OptionsWithTZ = {
     7│   weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6,

That error is coming from within date-fns-tz when flow evaluates its types.

I thought it was perhaps an issue with the version of date-fns but that appears not to be the case because even the earliest version allowed by the peerDependency range does not have Locale as an exported type.

Hopefully that helps. I tried my hand at updating with a PR myself, but when I found that Locale was not exported at all from date-fns, I figured it would probably take some higher level decision-making about where that type should be defined. Perhaps a pull request against date-fns to export that type might be in order.

marnusw commented 5 years ago

Thank you @chasingmaxwell for the thorough clarification. I will investigate further.

maniator commented 5 years ago

@marnusw We are getting the same issue as @chasingmaxwell

The type needs to changes to

 import type { Locale } from 'date-fns/locale'

Then everything will work

cc @kangax

maniator commented 5 years ago

This same issue exists for all flow types for this project (as the flow types for date-fns 2.x use date-fnx/<thing> and not a general flow type for the index.

saiichihashimoto commented 4 years ago

The Interval type is also not being exported from date-fns. It's not a type that date-fns-tz requires, but it would be useful to use the type defined by date-fns in client code rather than redefining the type ourselves.

marnusw commented 4 years ago

Thank you @maniator for the proposed fix. I have changed this in 1.0.8-beta.1. Please confirm whether this fixes the issue with Flow and I'll publish a proper release.

@saiichihashimoto I am not a maintainer of date-fns. If you would like additional types exposed please file an issue on the date-fns project.

maniator commented 4 years ago

Thank you @maniator for the proposed fix. I have changed this in 1.0.8-beta.1. Please confirm whether this fixes the issue with Flow and I'll publish a proper release.

@saiichihashimoto I am not a maintainer of date-fns. If you would like additional types exposed please file an issue on the date-fns project.

Hmmm We no longer have that original issue, but now it just says

Cannot resolve module date-fns-tz.

when we run flow @marnusw

marnusw commented 4 years ago

I have no idea why this is happening and won't be able to investigate this myself since I have never used Flow. I will happily try out any further suggestions, though. I'm guessing editing the installed files in node_modules until the Flow types are fixed could be a way to go, then post the solution here and I'll update the source (or, even better, submit a PR).

With Typescript there was a problem with importing the Locale type globally which then changed the type definition from global to module scope. Maybe that is the issue here as well. It could explain why the date-fns-tz module is not resolved at all. With Typescript the solution was importing inside the module declaration block rather than at the top of the file, but I'm not sure what the equivalent would be for Flow.