Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在typescript中将类型本地添加到非类型化的npm模块?_Typescript_Npm - Fatal编程技术网

如何在typescript中将类型本地添加到非类型化的npm模块?

如何在typescript中将类型本地添加到非类型化的npm模块?,typescript,npm,Typescript,Npm,我知道至少从2017年起,这一问题就在别处得到了解答,但我无法做到这一点。我在项目中的tsconfig.json中有noImplicitAny:true。我正在尝试使用clamscan,它既不是本机键入的,也不是@types中可用的。我的问题不是针对这个方案的 因此,当我在code.ts中从“clamscan”导入NodeClam时,我得到以下错误: Could not find a declaration file for module 'clamscan'. '.../node_module

我知道至少从2017年起,这一问题就在别处得到了解答,但我无法做到这一点。我在项目中的
tsconfig.json
中有
noImplicitAny:true
。我正在尝试使用
clamscan
,它既不是本机键入的,也不是
@types
中可用的。我的问题不是针对这个方案的

因此,当我在
code.ts
中从“clamscan”导入NodeClam时,我得到以下错误:

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
}
我还尝试了简单地
声明模块'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)
另一方面,
声明模块'*'
被编译器接受,但不是我想要的(并且无论如何也不能解决原始错误)。那么,如何输入非类型化的外部npm模块

我知道我可以为
定义类型
clamscan
软件包做出贡献,但问题在于仅为我的项目在本地注释/扩充软件包。

尝试以下方法:

declare module 'clamscan' {
   import * as Clamscan from 'clamscan';
  export default Clamscan
}
或者创建一个单独的文件,在“clamscan.d.ts”中只包含此文件


如果这不起作用,请查看此

我怀疑typescript找不到您的声明文件。尝试在路径
src/@types/clamscan/index.d.ts
src/@types/clamscan.d.ts
中输入。或者编辑
.tsconfig
中的
typeroot
属性,以包含存储它的路径。如果我在模块导入后声明了类型,则会出现“无效模块…”错误。谢谢提示。无法使它在src/中的任何位置工作,但我现在将它放在node_modules/@types/clamscan/index.d.ts中。要玩typeRoots看看我是否能做得更好。每个人都能解决这个问题吗?@DouglasGaskell不是真的,我在node_modules/@types中工作过,这显然并不理想,所以我最终将我的类型定义合并到了上游包中,以便对其他人有用-所以我想问题已经解决了,但是我没有一个解决我最初问题的技术方案
declare module 'clamscan' {
       import * as Clamscan from 'clamscan'; // or import Clamscan from "clamscan" or import {Clamscan} from "clamscan" . It depends of how the import is handled
    }