我使用的是fingerprintjs2库,它在definitelyTyped中具有不完整和不兼容的声明
我选择在我的项目中写我自己的声明,我很难用代码加载声明。我还在犯错误
找不到模块“fingerprintjs2”
在这个导入语句中
import Fingerprint2, { TCallback, TComponent } from 'fingerprintjs2';我的声明是作为独立模块编写的。
// ./src/@types/fingerprintjs2/index.d.ts
// ...some other exported types
export type TComponent = {
key: string;
value: string | number | boolean | string[];
};
export type TCallback = (components: TComponent[]) => void;
export default interface fingerprintjs2 {
get(callback: TCallback): void;
get(options: TOptions, callback: TCallback): void;
getPromise(options: TOptions): Promise<TComponent[]>;
}我的tsconfig.json
{
"include": ["src/**/*"],
"exclude": ["node_modules"],
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"declaration": true,
"declarationDir": "./dist",
"lib": ["dom", "es5", "scripthost"],
"module": "es6",
"noImplicitAny": true,
"outDir": "./dist",
"paths": { "*": ["./src/@types*"] },
"target": "es5"
}
}我没有找到关于这将如何运作的明确解释。你能给我提个建议吗?谢谢你的帮助。
我正在使用Webstorm 2018.2中的类型记录3.1.6
发布于 2018-11-09 13:43:07
问题是在解决模块时,我更改了tscongif.json编译器选项
++ "moduleResolution": "node",
-- "paths": { "*": ["./src/@types*"] },
++ "paths": { "*": ["./node_modules/*", "./src/@types/*"] },然后模块就被解析了。
我得到了一个错误,模块导出的只是类型,这是正确的,我导出了一个interface。我必须首先实现该接口,然后导出已实现的值。像这样
// declare const value with interface
declare const fingeprint: fingerprintjs2;
// export that const value
export default fingeprint;现在看来起作用了。
发布于 2018-11-09 12:17:09
您需要设置typeRoots,以便它能够选择您的自定义类型文件夹。
来源:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
像这样的事情应该有效:
"typeRoots": [
"node_modules/@types",
"src/@types"
]https://stackoverflow.com/questions/53225496
复制相似问题