typescript - TS7016 : Could not find a declaration file for module 'faker/locale/en_CA'

标签 typescript

我收到此错误:

TS7016: Could not find a declaration file for module 'faker/locale/en_CA'. '.../myproject/node_modules/faker/locale/en_CA.js' implicitly has an 'any' type.

使用此代码:

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 找不到。

最佳答案

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

关于typescript - TS7016 : Could not find a declaration file for module 'faker/locale/en_CA' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642159/

相关文章:

javascript - 如何填充嵌套在 Mongoose 对象数组中的文档?

typescript - 如何在 typescript 中处理字符串枚举与不同字符串文字的反向映射?

visual-studio - 尝试对 "$(lastknowntypescriptversion) that evaluates to "进行数字比较“而不是数字

node.js - 初始化时出错 : No matching bindings found for serviceIdentifier: Symbol(LicencesService)

javascript - Angular 2 使用不同的 url 路由到同一组件

angular - 在 IIS 中托管 Angular 4 应用程序后出现 HTTP 错误 500.19 内部服务器错误

javascript - 如何在for循环中循环 promise 并在满足条件时中断

angular - 设置 Angular 2 下拉列表中所选内容的值

Angular 4 在不更改 URL 的情况下解析保护重定向?

javascript - Angular 6 : How to set response type as text while making http call