代码之家  ›  专栏  ›  技术社区  ›  Alexander Crichton

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

  •  2
  • Alexander Crichton  · 技术社区  · 9 年前

    索引.tsx

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

    react moment 进口罚款。问题在于 react-big-calendar react-modal 这基本上就是我经历的过程

    我安装了 @types/react-modal .

    node_modules/@types/react model/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> {
        ...
    }
    

    那我可以用 从“反应模式”导入反应模式 ,它成功了。

    具有 我刚卸载 @types/react-big-calendar 现在有我自己的打字机了。毫无疑问,其他人正在使用这些声明文件来实现这些包,而不需要这么麻烦。如有任何指导,我们将不胜感激。

    {
        "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"
        ]
    }
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   GaryBishop    9 年前

    我想 react-modal/index.d.ts 有一个bug,你(或我)应该提交一个pull请求。如果我解释他们的 react-modal-tests.tsx import * as ReactModal from 'react-modal'; 它编译但不运行。我的印象是他们的测试只是编译。

    我知道了如何在不修改node\u包/@类型中的文件方式的情况下使其工作。

    compilerOptions "baseUrl": "src/types", "typeRoots": [ "./src/types", "./node_modules/@types" ]

    然后把你的修改 react-modal/index.d.ts src/types/react-modal/index.d.ts 。然后typescript编译器应该在那里找到它。我只是复制了 node_modules/@types/react-modal src/types/react-modal

    推荐文章