typescript - 如何正确的 "declare"一个模块? (TS7016)

标签 typescript

首先,这是我的 tsconfig.json:

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

现在我尝试导入一个 JavaScript 库:

 import Napi from '@org/napi-js';

我得到这个错误:

TS7016: Could not find a declaration file for module 'napi-js'. '/home/mpen/Projects/my-project/node_modules/@org/napi-js/dist/index.js' implicitly has an 'any' type. Try npm install @types/org__napi-js if it exists or add a new declaration (.d.ts) file containing `declare module 'org__napi-js';

@types/org__napi-js 不存在,所以这不好。那么如何正确声明呢?

我已经尝试创建一个文件,types/org__napi-js.d.ts',里面只有这个:

declare module 'org__napi-js';

然后我尝试更新我的 tsconfig.json 以便它可以找到它:

{
    "compilerOptions": {
        ...
        "baseUrl": "./",
        "paths": {
            "*": ["types/*"]
        }
    },
    ...

但这并没有帮助。

我该怎么办?或者,更明确地说:

  1. 我如何告诉 TypeScript 在哪里可以找到我的模块?
  2. 要说默认导出是 any,此模块中至少需要多少代码?

我想我想把所有这些 stub 放在 types/ 中,而我的源代码放在 src/ 中,除非有一些标准的方法来组织 TS 项目。

最佳答案

创建空声明的替代方法是 ts-ignore:

// @ts-ignore
import Napi from '@org/napi-js';

关于typescript - 如何正确的 "declare"一个模块? (TS7016),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51638818/

相关文章:

javascript - react JS |无法在现有状态转换期间更新

typescript - 如何为导出函数的commonjs模块编写定义文件

typescript - 从已解析的 Promise 返回 json 对象并在加载后分配它

javascript - 我计算得到该月的第一天,但​​得到了最后一天

javascript - TypeScript 包含流程

javascript - 具有不同类型数量的 typescript 接口(interface)

TypeScript:命名空间抛出 "property does not exist"

typescript - 如何查找 Vuejs 项目的所有未翻译字符串?

javascript - React-Typescript Hello World

Angular 4 Ngx-datatable Filter not working object is null but accessed (even with if condition)