首页 > 解决方案 > 打字稿无法解析类型

问题描述

我正在尝试在项目中使用 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"
    ]
}

标签: typescripttypescript-typingslog4javascript

解决方案


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

如果您指定target: "es2015",则模块系统将为es2015. 如果模块系统是es2015模块的默认分辨率,clasic则不查找node_modules类型(如此所述)。您可以在编译器选项的文档中找到它


推荐阅读