无法使用React-Day-Picker中的DayPickerInput和TypeScript

时间:2017-07-04 11:14:57

标签: reactjs typescript react-day-picker

每当我尝试导入如下的DayPickerInput时:

import DayPickerInput from 'react-day-picker/DayPickerInput';

我收到了ts警告:“找不到模块'react-day-picker / DayPickerInput的声明文件。”

查看模块文件夹,看起来只有DayPicker定义了类型。

当我尝试使用require方法时,如下所示:

var DayPickerInput = require('react-day-picker').DayPickerInput;

我的项目构建正常,但是当需要显示组件时,我在控制台中遇到了2个运行时错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. 

DayPickerInput现在无法使用。

3 个答案:

答案 0 :(得分:0)

react-day-picker v6.2为输入组件添加了TypeScript定义。尝试升级以查看它是否解决了您的问题。

答案 1 :(得分:0)

在7.0.7版本中,我不得不将其添加到globals.d.ts作为解决方法:

declare module 'react-day-picker/DayPickerInput' {
  import { DayPickerInput } from 'react-day-picker/types/DayPickerInput';
  export default DayPickerInput;
}

我的印象是TypeScript定义不是最新的,有人(你或我?)需要更新它们? :)

答案 2 :(得分:0)

解决方案:如果您也遇到此问题,请检查您的导入中是否存在 react-day-picker 和 DayPickerInput 之间的“类型”。

导入的可视化代码:

  import DayPickerInput from 'react-day-picker/types/DayPickerInput';

正确的导入:

  import DayPickerInput from 'react-day-picker/DayPickerInput';

出现此问题的版本:

package.json(相关包)

  "dependencies": {
    { ... },
    "react-day-picker": "7.4.8",
  },
  "devDependencies": {
    { ... },
    "@types/react-day-picker": "5.3.0",
    "typescript": "4.1.3",
  }