【问题标题】:typescript unable to resolve type打字稿无法解析类型
【发布时间】:2019-04-16 08:16:27
【问题描述】:

我正在尝试在一个项目中使用 log4javascript,我发现该包确实在 node_modules/log4javascript/ 中存在 log4javascript.d.ts,并且 package.json 确实在“typings”属性中引用了它,但是在编译我的它抱怨的项目:

S2307:找不到模块“log4javascript”

我正在使用以下方式导入模块:

import {getLogger } from 'log4javascript';

据我了解,不需要单独安装类型:

npm install @types/log4javascript

因为打字已经存在。但是如果我有,我不确定如何使用模块类型和方法:

"noImplicitAny": true,

在我的 tsconfig.json 中设置

我的 tsconfig.json 看起来像:

{
    "compileOnSave": true,
    "compilerOptions": {
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "target": "es2015",
        "declaration": true
    },
    "include": [
        "static/js/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

【问题讨论】:

    标签: typescript typescript-typings log4javascript


    【解决方案1】:

    您需要在tsconfig.json 中指定"moduleResolution": "node" 选项

    如果您指定target: "es2015",则模块系统将为es2015。如果模块系统是es2015,那么模块的分辨率默认为clasic,它不会在node_modules 中查找类型(如here 所述)。你可以在编译器的文档中找到这个options

    【讨论】:

    • 感谢您的回答。我想知道你什么时候使用什么模块系统?我正在构建一个项目,该项目将在支持 es2015 的浏览器中运行。
    • @Divick 目前我正在开发一个 Angular 项目,我只是使用 ng create 为我设置的内容,即 "module": "es2015""moduleResolution": "node"
    猜你喜欢
    • 2020-05-21
    • 2019-08-18
    • 2018-09-07
    • 1970-01-01
    • 2023-02-07
    • 2017-01-22
    • 2019-02-07
    • 2019-11-01
    • 2018-07-30
    相关资源
    最近更新 更多