【问题标题】:TypeScript - Problems importing some npm packages that use declaration files (typings) from DefinitelyTypedTypeScript - 导入一些使用来自definitelyTyped 的声明文件(类型)的 npm 包时出现问题
【发布时间】:2017-12-01 21:25:53
【问题描述】:

对于我要导入的一些模块,我最终会修改它们的声明文件以使其正常工作。我不确定我是否做错了,或者这些模块的类型是否恰好有问题。

index.tsx

import * as React from "react";
import * as moment from "moment";
import BigCalendar from "react-big-calendar";
import ReactModal from "react-modal";

reactmoment 导入正常。问题在于react-big-calendarreact-modal。这基本上是我为react-modal经历的过程:

我安装了@types/react-modal

node_modules/@types/react-modal/index.d.ts(缩写)

// Type definitions for react-modal 1.6
// Project: https://github.com/reactjs/react-modal
// Definitions by: Rajab Shakirov <https://github.com/radziksh>, Drew Noakes <https://github.com/drewnoakes>, Thomas B Homburg <https://github.com/homburg>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import * as React from "react";

export as namespace ReactModal;

export = ReactModal;

declare namespace ReactModal {
    interface Props {
        ...
    }
}

declare class ReactModal extends React.Component<ReactModal.Props> {
    ...
}

我尝试了import ReactModal from "react-modal",它给了我Module '...' has no default export

我试过import { ReactModal } from "react-modal" 这给了我Module '...' has no exported member 'ReactModal'

我尝试了import * asReactModal from "react-modal",它可以编译,但在运行时给了我Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object

最终我只是将声明文件更改为:

import * as React from "react";

export as namespace ReactModal;

declare namespace ReactModal {
    interface Props {
        ...
    }
}

export default class ReactModal extends React.Component<ReactModal.Props> {
    ...
}

然后我可以使用import ReactModal from "react-modal",它起作用了。

使用react-big-calendar,我刚刚卸载了@types/react-big-calendar,现在有我自己的类型。当然,其他人正在为这些包使用这些声明文件而没有这些麻烦。任何指导将不胜感激。

package.json

{
    "dependencies": {
        "@types/react": "^15.0.27",
        "@types/react-dom": "^15.5.0",
        "@types/react-modal": "^1.6.7",
        "moment": "^2.18.1",
        "react": "^15.5.4",
        "react-big-calendar": "^0.14.0",
        "react-dom": "^15.5.4",
        "react-modal": "^2.1.0"
    },
    "devDependencies": {
        "awesome-typescript-loader": "^3.1.3",
        "babel-core": "^6.24.1",
        "babel-loader": "^7.0.0",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-react": "^6.24.1",
        "css-loader": "^0.28.4",
        "source-map-loader": "^0.2.1",
        "style-loader": "^0.18.2",
        "typescript": "^2.3.4",
        "webpack": "^2.5.0"
    }
}

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./Scripts/dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "target": "es5",
        "jsx": "react"
    },
    "include": [
        "./Scripts/src/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

【问题讨论】:

  • 您的类型未正确安装:来自文档的npm install --save-dev @types/node
  • 感谢@OrcusZ。我已经解决了。我认为这不是为了解决这个特定问题?
  • 我在更新我的包后遇到了同样的问题。
  • 您对 react-modal/index.d.ts 的编辑对我有用。在节点包中向下编辑定义似乎真的是错误的。一定有更好的办法。

标签: typescript definitelytyped react-modal react-big-calendar


【解决方案1】:

我认为react-modal/index.d.ts 有一个错误,你(或我)应该提交一个拉取请求。如果我正确解释了他们的react-modal-tests.tsx,他们会使用import * as ReactModal from 'react-modal'; 将其导入,该import * as ReactModal from 'react-modal'; 可以编译但不运行。我的印象是他们的测试只能编译。

我想出了如何在不修改 node_packages/@types 中的文件的情况下让它工作。

将这些行添加到 tsconfig 文件的 compilerOptions 部分: "baseUrl": "src/types", "typeRoots": [ "./src/types", "./node_modules/@types" ]

然后将修改后的react-modal/index.d.ts 放入src/types/react-modal/index.d.ts。然后打字稿编译器应该在那里找到它。我只是将node_modules/@types/react-modal 的内容复制到src/types/react-modal 并进行了更改。

【讨论】:

  • 谢谢加里。对延迟回复您表示歉意。我怀疑声明可能存在问题,但我对 TypeScript 不够熟悉,无法自己得出结论。当我在两组不同的声明中遇到同样的问题时,我更加怀疑自己。我现在在我的源代码中有自己的类型,并且已经卸载了 npm @type 包。
猜你喜欢
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 2017-07-21
  • 2021-06-06
  • 1970-01-01
  • 2020-04-09
  • 2016-03-12
  • 1970-01-01
相关资源
最近更新 更多