【问题标题】:TS7016: Could not find a declaration file for module 'faker/locale/en_CA'TS7016:找不到模块“faker/locale/en_CA”的声明文件
【发布时间】:2019-01-09 13:10:19
【问题描述】:

我收到此错误:

TS7016:找不到模块“faker/locale/en_CA”的声明文件。 '.../myproject/node_modules/faker/locale/en_CA.js' 隐含了一个 'any' 类型。

使用此代码:

import * as faker from 'faker/locale/en_CA';

问题是,我安装了@types/faker,我可以看到node_modules/@types/faker/index.d.ts实际上包括:

declare module "faker/locale/en_CA" {
    export = fakerStatic;
}

所以模块已经被声明了,但是TS由于某种原因找不到它。

这是我的 tsconfig:

{
    "compilerOptions": {
        "strict": true,
        "importHelpers": false,
        "inlineSources": true,
        "noEmitOnError": true,
        "pretty": true,
        "module": "commonjs",
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true,
        "removeComments": true,
        "preserveConstEnums": false,
        "sourceMap": true,
        "lib": ["es2017","esnext.asynciterable"],
        "skipLibCheck": true,
        "outDir": "dist",
        "target": "es2018",
        "declaration": true,
        "types" : ["node"],
        "resolveJsonModule": true,
        "esModuleInterop": false,
        "baseUrl": ".",
        "paths": {
            "*": ["types/*"]
        }
    },

    "files": [
        "src/main"
    ],
    "exclude": [
        "node_modules"
    ]
}

我错过了什么吗?


注意

import * as faker from 'faker';

工作得很好。

VSCode 和 PhpStorm 都可以找到 'faker/locale/en_CA' 没问题,只是 tsc 不能。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    尝试从 tsconfig.json 中删除 "types" : ["node"]。根据the documentation,该行表示您不希望TypeScript 为node 以外的任何包加载类型。

    【讨论】:

    • 是的。做到了。以为我需要那个。也许那是在我将@types/node 添加到我的项目之前?我想添加@types/xxx 是首选的做事方式吗?奇怪的是它接受了根“faker”包但不接受“faker/locale/en_CA”,如果它只是在寻找“node”。
    猜你喜欢
    • 2021-07-24
    • 2019-10-22
    • 2020-03-30
    • 2017-08-13
    • 2022-11-10
    • 1970-01-01
    • 2021-01-02
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多