date-fns: Typings's broken on latest Typescript (2.5.3)

I am not sure why but something in v2.5.3 has broken compilation of my project.

Could not find a declaration file for module 'date-fns/format'. '/path/to/my/project/node_modules/date-fns/format/index.js' implicitly has an 'any' type.

I’m using date-fns like this

import * as format from 'date-fns/format'

For now, I’ve reverted it back to Typescript v2.5.2 as it is working with no problems.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 22
  • Comments: 21 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hey guys, a quick fix is using the require statement:

const fromat = require('date-fns/format/index')

@kossnocorp @leshakoss we need fixed typings 😄 could you please make a release?

@kossnocorp I’m just now running into this issue in v1. It looks like a fix was ready back in November, but hasn’t been released yet, as far as I can tell. Is there anything in particular holding up a release?

Looks like both have been merged. Can you give an idea when a new release will be available?

According to feedback from typescript maintainers:

A solution that should work in both old and new versions of TypeScript would be to create a file date-fns/format/index.d.ts like is done with date-fns/format/index.js.flow, instead of adding many declare module in date-fns/typings.d.ts. Then there would be no need for adding typings to package.json since the definitions would be where we expect them to be anyway.

And I tested it locally, it should work by creating index.d.ts file in node_modules/date-fns/format/ with following code:

declare module 'date-fns/format' {
    import { format } from 'date-fns'
    export = format
}

Is there someone that can review #598 and #599 ? This issue need to be fixed.

temporary fix is easy enough:

just use type path mapping to your custom override of format

tsconfig.json

{
  "baseUrl": "./",
  "paths": {
      "date-fns/format": [
        "manual_typings/date-fns/format"
      ]
    }
}
  /manual_typings
  | - /date-fns
  |    | - /format
  |    |    |- index.d.ts
// manual_typings/date-fns/format/index.d.ts
declare module 'date-fns/format' {
  import {format} from 'date-fns'
  export = format
}