【问题标题】:@types/prop-types/index has no default export@types/prop-types/index 没有默认导出
【发布时间】:2017-12-05 13:32:43
【问题描述】:

我正在尝试使用https://github.com/facebook/prop-types

所以我也为它安装了@types/prop-types。 https://www.npmjs.com/package/@types/prop-types

但我猜这个错误。 [ts] 模块 '"/node_modules/@types/prop-types/index"' 没有默认导出。

我想要完成的是 withRouter 文档中正在做的事情。 https://reacttraining.com/react-router/web/api/withRouter

例如,您在他们的 JavaScript 中看到 PropTypes 的使用:

import React from 'react'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'

// A simple component that shows the pathname of the current location
class ShowTheLocation extends React.Component {
  static propTypes = {
    match: PropTypes.object.isRequired,
    location: PropTypes.object.isRequired,
    history: PropTypes.object.isRequired
  }

  render() {
    const { match, location, history } = this.props

    return (
      <div>You are now at {location.pathname}</div>
    )
  }
}

// Create a new component that is "connected" (to borrow redux
// terminology) to the router.
const ShowTheLocationWithRouter = withRouter(ShowTheLocation)

对此的任何帮助表示赞赏!

【问题讨论】:

    标签: reactjs typescript react-router-v4


    【解决方案1】:

    你需要像这样修改你的导入语句

    import * as PropTypes from 'prop-types'
    

    这里的意思是创建一个对象PropTypes,并将prop-types模块中的所有导出导入PropTypes对象。

    【讨论】:

    • 我对我所有的 import 声明都做了同样的事情
    • 这确实解决了警告,但我不喜欢触摸源代码的方式。希望有办法通过 babel 修复它,比如 babel 如何解决 lodash tree-shaking 问题。
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2017-11-11
    • 2021-10-15
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2018-09-15
    相关资源
    最近更新 更多