1

I've been trying to write a custom declaration file for the 'react-dates' npm module, but can't get the compiler to resolve my declaration file to that module.

When I do import {DateRangePicker} from 'react-dates' I get the following error:

Could not find a declaration file for module 'react-dates'. 'absolute_path/src/node_modules/react-dates/index.js' implicitly has an 'any' type.

My declaration file is located in the path '@types/react-dates/index.d.ts' and looks like this:

import * as React from 'react';
declare class DateRangePicker extends React.Component<{}, {}> { }

The tsconfig.json looks like this:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react",
    "typeRoots": [
      "./@types"
    ]
  },
  "include": [
    "./app/**/*",
    "./@types/**/*"
  ]
}
torryt
  • 150
  • 10

1 Answers1

3

Solved it by wrapping it in a module statement like this:

declare module 'react-dates' {
  import { Component } from 'react';
  export class DateRangePicker extends Component<{}, {}> { }
}

Note that the import statements have to be inside the module block, or else it returns:

Invalid module name in augmentation. Module 'react-dates' resolves to an untyped module at 'path/node_modules/react-dates/index.js', which cannot be augmented.

torryt
  • 150
  • 10