【问题标题】:Could not find declaration file with custom declaration file for 'react-dates' module找不到带有“react-dates”模块的自定义声明文件的声明文件
【发布时间】:2017-06-28 19:40:32
【问题描述】:

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

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

找不到模块“react-dates”的声明文件。 'absolute_path/src/node_modules/react-dates/index.js' 隐式具有“任何”类型。

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

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

tsconfig.json 看起来像这样:

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

【问题讨论】:

标签: typescript typescript-typings


【解决方案1】:

通过将它包装在这样的模块语句中来解决它:

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

注意 import 语句必须在模块块内,否则返回:

扩充中的模块名称无效。模块“react-dates”解析为“path/node_modules/react-dates/index.js”处的无类型模块,无法扩充。

【讨论】:

  • 你就是那个男人!我一直在寻找这个答案几个小时。
猜你喜欢
  • 1970-01-01
  • 2019-03-16
  • 2019-07-26
  • 2021-02-21
  • 2017-05-29
  • 1970-01-01
  • 2022-06-12
  • 2022-11-06
  • 2017-08-13
相关资源
最近更新 更多