简体   繁体   English

TypeScript | 尝试导入依赖项时遇到错误

[英]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 containing declare 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.

相关问题 尝试导入 html 文件时出现 Typescript 错误 - Typescript error when trying to import an html file 尝试导入iView时未定义错误 - iView not defined error while trying to import it 尝试将用打字稿编写的类导入到显示错误的 javascript NodeJs - Trying to import a class written in typescript to javascript NodeJs showing error 在打字稿中调用rest API时,面临错误,因为“'this'隐式具有'any'类型,因为它没有类型注释” - Facing error as "'this' implicitly has type 'any' because it does not have a type annotation" while calling rest API in typescript 引用错误:使用typescript时在引导程序切换中导入jQuery - Reference error: import jQuery in bootstrap toggle while using typescript 在尝试启动反应应用程序时,我面临以下错误并且它没有编译为什么? - While trying to start react app i am facing the below error and it is not compiling why? 尝试在打字稿文件中调用函数时模块未定义错误 - Module is not defined error while trying to call function in typescript file 导入模块打字稿错误 - import module typescript error 键入脚本,尝试导入 localForage 时出现运行时错误 - type script, runtime error while trying to import localForage 尝试从另一个 js 文件导入函数时出错 - Error while trying to import a function from another js file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM