简体   繁体   English

TypeScript-从DefinitelyTyped导入一些使用声明文件(类型)的npm软件包时出现问题

[英]TypeScript - Problems importing some npm packages that use declaration files (typings) from DefinitelyTyped

For some of the modules I'm importing I end up modifying their declaration files to get them working. 对于我要导入的某些模块,我最终修改了它们的声明文件以使它们正常工作。 I'm not sure if I'm going about it wrong or if there just happen to be issues with the typings for these modules. 我不确定是否要解决这个问题,或者这些模块的类型是否恰好存在问题。

index.tsx index.tsx

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

react and moment import fine. reactmoment导入罚款。 The problems are with react-big-calendar and react-modal . 问题在于react-big-calendarreact-modal This is basically the process I went through for react-modal : 这基本上是我完成react-modal

I installed @types/react-modal . 我安装了@types/react-modal

node_modules/@types/react-modal/index.d.ts (abbreviated) 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> {
    ...
}

I tried import ReactModal from "react-modal" which gives me Module '...' has no default export . 我尝试import ReactModal from "react-modal"这给了我Module '...' has no default export

I tried import { ReactModal } from "react-modal" which gives me Module '...' has no exported member 'ReactModal' . 我尝试import { ReactModal } from "react-modal"这使我Module '...' has no exported member 'ReactModal'

I tried import * asReactModal from "react-modal" which compiles, but gives me Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object at run time. 我尝试import * asReactModal from "react-modal"编译的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: objectWarning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object在运行时。

Eventually I just altered the declaration file to this: 最终,我将声明文件更改为:

import * as React from "react";

export as namespace ReactModal;

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

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

Then I could use import ReactModal from "react-modal" , and it worked. 然后,我可以使用import ReactModal from "react-modal" ,它可以正常工作。

With react-big-calendar I just uninstalled @types/react-big-calendar and now have my own typings for it. 使用react-big-calendar我刚刚卸载了@types/react-big-calendar ,现在有了自己的类型。 Surely other people are using these declaration files for these packages without all this hassle. 毫无疑问,其他人正在为这些软件包使用这些声明文件,而没有所有这些麻烦。 Any guidance would be appreciated. 任何指导将不胜感激。

package.json 的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 tsconfig.json

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

I think react-modal/index.d.ts has a bug and you (or I) should submit a pull request. 我认为react-modal / index.d.ts有一个错误,您(或我)应该提交拉取请求。 If I interpret their react-modal-tests.tsx correctly, they import it with import * as ReactModal from 'react-modal'; 如果我正确解释了他们的react-modal-tests.tsx ,他们会用import * as ReactModal from 'react-modal';import * as ReactModal from 'react-modal'; which compiles but doesn't run. 可以编译但无法运行。 My impression is their tests only compile. 我的印象是他们的测试只能编译。

I figured out how to get it to work without modifying the file way down in node_packages/@types. 我想出了如何使其工作而又不修改node_packages / @ types中的文件的方法。

Add these lines to the compilerOptions section of the tsconfig file: "baseUrl": "src/types", "typeRoots": [ "./src/types", "./node_modules/@types" ] 这些行添加到compilerOptions的tsconfig文件的部分: "baseUrl": "src/types", "typeRoots": [ "./src/types", "./node_modules/@types" ]

Then put your modified react-modal/index.d.ts in src/types/react-modal/index.d.ts . 然后将修改后的react-modal/index.d.ts放在src/types/react-modal/index.d.ts The typescript compiler should then find it there. 然后,打字稿编译器应该在那里找到它。 I simply copied the contents of node_modules/@types/react-modal to src/types/react-modal and made your changes. 我只是将node_modules/@types/react-modal的内容复制到src/types/react-modal并进行了更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 npm @types org包中的TypeScript类型 - TypeScript typings in npm @types org packages 我如何才能将JavaScript声明打字稿文件中的static方法和实例方法与defineType区别开来? - How could I differentiate between static methods and instance methods in javascript declaration typescript files from definitelyTyped? 如何使用新的@types包键入TypeScript? - How to use the new @types packages for TypeScript typings? 如何在 typescript 中使用来自definitelyTyped 的下划线库? - How to use underscore lib from DefinitelyTyped with typescript? 如何在TypeScript中从DefinitelyTyped扩展类型声明 - How do I extend a type declaration from DefinitelyTyped in TypeScript 使用TypeScript导入自定义声明文件 - Importing custom declaration files with TypeScript 在Typescript 2.0中,如何为@types npm命名空间(DefinitelyTyped存储库)中不可用的模块提供单个声明文件? - In typescript 2.0, how to have a single declaration file for modules not available in the @types npm namespace (DefinitelyTyped repo)? 如何解决PouchDB的TypeType / DefinitelyTyped类型错误(?)? - How to work around incorrect (?) TypeScript/DefinitelyTyped typings for PouchDB? 使用TypeScript中声明的模块的输入 - Use typings from a declared module in TypeScript 加载 RxJS Typescript 类型而不从 CommonJS 模块导入? - load RxJS Typescript typings without importing from CommonJS module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM