简体   繁体   English

在 Typescript 项目中使用 Javascript 库

[英]Use Javascript Libary On Typescript Project

I want to use an payment system which is calling iyzico.我想使用调用 iyzico 的支付系统。 The iyzico payment library is https://www.npmjs.com/package/iyzipay it. iyzico 支付库是https://www.npmjs.com/package/iyzippay它。 But there is not any types so how can i use this library on my node.js typescript express backend project?但是没有任何类型,所以我如何在我的 node.js typescript express 后端项目上使用这个库?

i tried make one mylib.d.ts file on my root directory and put this code on this file.我尝试在我的根目录上创建一个 mylib.d.ts 文件并将此代码放在此文件中。

declare module 'iyzipay';

After then i put this variable on my tsconfig.json file然后我把这个变量放在我的 tsconfig.json 文件中

  ,"include": [
    "src", "mylib.d.ts"
  ]

When i import the libary on my project i get an error like this.当我在我的项目中导入库时,我收到这样的错误。

Could not find a declaration file for module 'iyzipay'.找不到模块“iyzipay”的声明文件。 '/Users/xxx/node_modules/iyzipay/lib/Iyzipay.js' implicitly has an 'any' type. '/Users/xxx/node_modules/iyzipay/lib/Iyzipay.js' 隐含了一个 'any' 类型。 Try npm i --save-dev @types/iyzipay if it exists or add a new declaration (.d.ts) file containing `declare module 'iyzipay';如果存在,请尝试npm i --save-dev @types/iyzipay或添加包含 `declare module 'iyzipay' 的新声明 (.d.ts) 文件;

You need to declare export types for module 'iyzipay';您需要为模块“iyzipay”声明导出类型;

For example:例如:

declare module 'iyzipay' {

  interface IyzipayOptions {
    apiKey: string;
    secretKey: string;
    uri: string;
  }

  class Iyzipay {
    constructor (options: IyzipayOptions);
  }
  
  export default Iyzipay;
}

This type declaring is just for the simple usage from document .这种类型声明仅用于document的简单用法。 You should write the types as you need.您应该根据需要编写类型。

import Iyzipay from 'iyzipay';

var iyzipay = new Iyzipay({
    apiKey: 'your api key',
    secretKey: 'your secret key',
    uri: 'https://sandbox-api.iyzipay.com'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM