我知道自2017年以来已经有人提出并回答了类似的问题,但我无法让其正常工作。我在我的项目的`tsconfig.json`中设置了`noImplicitAny:true`。我正在尝试使用既不是原生类型也不在`@types`中可用的`clamscan`。我的问题不限于此程序包。
当我在`code.ts`中导入`NodeClam from 'clamscan'`时,我会收到以下错误:
我也尝试了简单地写成
当我在`code.ts`中导入`NodeClam from 'clamscan'`时,我会收到以下错误:
Could not find a declaration file for module 'clamscan'. '.../node_modules/clamscan/index.js' implicitly has an 'any' type.
Try `npm install @types/clamscan` if it exists or add a new declaration (.d.ts) file containing `declare module 'clamscan';`ts(7016)
因此,我创建了一个 clamscan.d.ts
文件,其中包含:
declare module 'clamscan' {
// the declarations I use
}
我也尝试了简单地写成
declare module 'clamscan';
,但两种情况都会出现以下问题:Invalid module name in augmentation. Module 'clamscan' resolves to an untyped module at '.../node_modules/clamscan/index.js', which cannot be augmented.ts(2665)
另一方面,declare module '*';
被编译器接受,但并不是我想要的(而且也无法解决原始错误)。那么,我该如何为未经类型标注的外部 npm 模块进行类型标注呢?
我知道我可以为 DefinitelyTyped
或者 clamscan
包做出贡献,但这个问题是关于仅为我的项目本地注释/增强包的。
src/@types/clamscan/index.d.ts
或src/@types/clamscan.d.ts
中。或者编辑你的.tsconfig
中的typeRoots
属性,将其包含在你存储它的路径中。如果我在导入模块之后声明类型,则会出现“无效模块…”错误。 - Linda Paiste