关于打字稿:找不到带有“ react-dates”模块的自定义声明文件的声明文件

Could not find declaration file with custom declaration file for 'react-dates' module

我一直在尝试为'react-dates'npm模块编写一个自定义声明文件,但是无法让编译器将我的声明文件解析为该模块。

当我执行import {DateRangePicker} from 'react-dates'时,出现以下错误:

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

我的声明文件位于路径" @ types / react-dates / index.d.ts"中,如下所示:

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

tsconfig.json如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
 "compilerOptions": {
   "outDir":"./dist/",
   "sourceMap": true,
   "noImplicitAny": true,
   "strictNullChecks": true,
   "module":"commonjs",
   "target":"es6",
   "jsx":"react",
   "typeRoots": [
     "./@types"
    ]
  },
 "include": [
   "./app/**/*",
   "./@types/**/*"
  ]
}


通过将其包装在如下模块语句中来解决该问题:

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

请注意,import语句必须位于模块块内,否则它将返回:

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.