首先,这是我的 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/*"]
}
},
...
但这并没有帮助。
我该怎么办?或者,更明确地说:
- 我如何告诉 TypeScript 在哪里可以找到我的模块?
- 要说默认导出是
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/