[英]TypeScript | Facing Error while trying to import a dependency
I am not used to using node.js and typescript.我不习惯使用node.js和typescript。
I tried importing Paytm dependency using the following code: npm install paytmchecksum
or by adding the below code in package.json我尝试使用以下代码导入 Paytm 依赖项: npm install paytmchecksum
或在 package.json 中添加以下代码
"dependencies": {
...
"paytmchecksum": "^1.5.0"
...
}
Now, I am using firebase, typescript and node.js. When I try importing, using现在,我正在使用 firebase、typescript 和 node.js。当我尝试导入时,使用
import PaytmChecksum from "../node_modules/PaytmChecksum";
import PaytmChecksum from "paytmchecksum";
or any other thing.或任何其他事情。 There are just errors.只有错误。 I believe maybe because the dependency is to be used with javascript rather than typescript.我相信可能是因为该依赖项将与 javascript 一起使用,而不是与 typescript 一起使用。
I get the following error:我收到以下错误:
Could not find a declaration file for module '../node_modules/PaytmChecksum'.找不到模块“../node_modules/PaytmChecksum”的声明文件。 'd:/firebase-functions/functions/node_modules/PaytmChecksum/PaytmChecksum.js' implicitly has an 'any' type. 'd:/firebase-functions/functions/node_modules/PaytmChecksum/PaytmChecksum.js' 隐式具有 'any' 类型。
sometimes I get this,有时我得到这个,
Could not find a declaration file for module 'paytmchecksum'.找不到模块“paytmchecksum”的声明文件。 'D:/firebase-functions/functions/node_modules/paytmchecksum/PaytmChecksum.js' implicitly has an 'any' type. 'D:/firebase-functions/functions/node_modules/paytmchecksum/PaytmChecksum.js' 隐式具有 'any' 类型。 Try
npm i --save-dev @types/paytmchecksum
if it exists or add a new declaration (.d.ts) file containingdeclare module 'paytmchecksum';
尝试npm i --save-dev @types/paytmchecksum
如果存在)或添加包含declare module 'paytmchecksum';
What is the workaround for this problem?此问题的解决方法是什么?
You either need to create your own type declarations file or use @ts-ignore on the line before the import.您需要创建自己的类型声明文件或在导入前的行中使用 @ts-ignore。
If you create your own type.d.ts file, you will need to make sure that it is included the project in your tsconfig.json project file.如果您创建自己的 type.d.ts 文件,您需要确保它包含在您的 tsconfig.json 项目文件中。
The type file can be as simple as one line (./mytypes/paytmchecksum.d.ts):类型文件可以像一行一样简单(./mytypes/paytmchecksum.d.ts):
declare module 'paytmchecksum';
This simply gets rid of the implied any and makes it explicit.这只是摆脱了隐含的 any 并使其明确。 You won't get any intellisense or type checking, but it will fix the error.您不会得到任何智能感知或类型检查,但它会修复错误。 You could go the extra mile and create a full type-definition file.您可以 go 加倍努力并创建完整的类型定义文件。 If you, do you should add it to the @types repository so others can use it.如果是,您是否应该将它添加到@types 存储库以便其他人可以使用它。
Then just add include to your tsconfig.json file:然后只需将 include 添加到您的 tsconfig.json 文件中:
{ "include": [
"mytypes/paytmchecksum.d.ts"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.