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/**/*"
  ]
}
2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're the man! I've been searching for this answer for hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.