Stripe + TypeScript:如何扩展条带节点的@types/stripe 定义? [英] Stripe + TypeScript: How to extend @types/stripe definitions for stripe-node?

查看:32
本文介绍了Stripe + TypeScript:如何扩展条带节点的@types/stripe 定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理项目,我们将 Stripe 库用于 Node.我们还想在这个项目中使用 TypeScript.

I'm working on project, where we're using Stripe library for Node. We also want to use TypeScript on this project.

我发现 Stripe 没有提供官方的 TypeScript 定义,但我发现了一些社区定义 @types/stripe.所以我安装了它们,过了一会儿我得到了一个错误:Stripe"类型不存在sources"属性.

I've figured out that Stripe isn't providing official TypeScript definitions but I've found some community definitions @types/stripe on NPM. So I installed them and after a while I got an error: Property 'sources' does not exist on type 'Stripe'.

好吧,缺少一些定义,例如对于这个 stripe.sources-相关功能.

Well there are missing some definitions, for example for this stripe.sources-related functionality.

我想在本地添加缺失的定义.所以我需要扩展这个文件:@types/stripe/index.d.ts

I want to add missing definitions locally. So I need to extend this file: @types/stripe/index.d.ts

我认为对于上述问题,我需要:

I think that for the problem above I need:

  • 将属性 sources: Stripe.resources.Sources; 添加到类 Stripe,
  • 将类Sources添加到命名空间resources
  • Sources 类添加缺少的函数声明.
  • to add property sources: Stripe.resources.Sources; to class Stripe,
  • to add class Sources to namespace resources,
  • to add missing function declarations to class Sources.

问题是我真的不知道怎么做.带有扩展名的 .d.ts 文件应该是什么样子的?我根据一些例子和 TypeScript 文档做了很多尝试,但它总是不起作用.你有什么想法吗?

The problem is that I really don't know how. How should the .d.ts file with extensions look like? I've made many attempts according some examples and TypeScript docs but it always doesn't work. Do you have any idea?

推荐答案

我认为我的同事找到了适合我的解决方案.他是这样做到的:

I think my colleague has found a solution that works for me. Here is how he made it:

import ST from 'stripe'

declare module 'stripe' {
    namespace sources {
        interface ISource extends IResourceObject {
            ...
        }

        interface ISourceCreationData {
            ...
        }
    }

    namespace resources {
        class Sources {
            create(data: sources.ISourceCreationData): Promise<sources.ISource>;
            retrieve(source: string, client_secret?: string): Promise<sources.ISource>;
        }
    }

    class Stripe extends ST {
        sources: ST.resources.Sources;
    }
}

这篇关于Stripe + TypeScript:如何扩展条带节点的@types/stripe 定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆