在我的TypeScript代码中,我想包含一个没有类型声明的NPM JavaScript模块。
由于这个原因,编译器会抱怨:
import createPersist = require('vuex-localstorage');
由于这个原因,编译器会抱怨:
error TS7016: Could not find a declaration file for module 'vuex-localstorage'. '<path>\node_modules\vuex-localstorage\dist\index.js' implicitly has an 'any' type.
解决这个问题的一个可能性是在我的 tsconfig.json
中将 "noImplicitAny"
设置为false
。但这不是我想要的。我想知道什么没有经过类型检查。
因此,我编写了一个最小的类型声明文件 vuex-localstorage
,并将其放在我的项目目录中:
interface PersistOptions {
namespace?: string;
initialState?: any;
provider?: any;
serialize?: (data:any) => string;
deserialize?: (str:string) => any;
expires?: number;
merge?: (args:any[]) => any;
reducer?: (state:any, paths:string[]) => any;
paths?: string[];
}
declare function createPersist(options: PersistOptions): any;
export = createPersist;
然而,我仍然遇到同样的错误。我尝试了几种方法来让TypeScript编译器识别我的类型声明文件,但是都没有成功。
那么我该怎么做呢?
tsconfig.json
中没有files:
或include
行,则默认情况下将包含它。如果您有files
或include
,则只需添加本地声明文件的路径即可。 - Aviad Hadad