【问题标题】:How to use stripe types in typescript如何在打字稿中使用条纹类型
【发布时间】:2020-05-11 05:23:03
【问题描述】:

我正在尝试使用环回框架创建一个应用程序,该框架使用打字稿。我想使用 Stripe,我同时安装了 stripe 和 @types/stripe,因为据我了解,Typescript 需要明确的类型。

我将条纹导入为 从'stripe'导入*作为条纹

我也想导入类型,所以我正在做 从'@types/stripe'导入*作为条纹

但是,我收到错误消息: 无法导入类型声明文件。考虑导入 'stripe' 而不是 '@types/stripe'

如何导入类型以便我可以使用它们来声明函数返回等?

【问题讨论】:

  • 您没有显式导入声明,只需从“stripe”导入名称。
  • 那么单独导入类型?像 import {ICustomer} from 'stripe' 还是在声明中做 stripe.ICustomer?

标签: typescript stripe-payments


【解决方案1】:

您通过安装 stripe 和 @types/stripe 做得正确。但是,您不需要在源文件中导入类型声明

import * as Stripe from 'stripe';

export const stripe = new Stripe('whateverkey');

function createHeader(): Stripe.HeaderOptions { // use stripe types for return type
  return 'stripe header';
}

如果你使用 VSCode 或 IDE 编辑器,它应该会给你 IntelliSense,如下所示

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果您直接使用stripe-node [1],它现在支持打字稿并已定义类型。对于那些发现这个并使用来自 stripe-node 的类型的人,您可以像下面这样导入 Stripe:

    import Stripe from 'stripe';
    const stripe = new Stripe(
      'sk_test_...', 
      {
        apiVersion: '2019-12-03',
        typescript: true,
      }
    );
    

    [1]https://github.com/stripe/stripe-node#usage-with-typescript

    【讨论】:

    • 非常感谢!所以我不再需要下载@types/stripe 了吗?
    • 不,这些是最近作为条带节点库的一部分提供的。
    猜你喜欢
    • 2019-09-26
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2021-04-03
    • 1970-01-01
    • 2020-04-17
    • 2018-01-31
    相关资源
    最近更新 更多