Vikram Khemlani
Vikram Khemlani

Reputation: 635

How to use stripe types in typescript

I'm trying to create an application using loopback framework, which uses typescript. I want to use Stripe, and I installed both stripe and @types/stripe, because from what I understand, Typescript needs definite types.

I'm importing stripe as import * as stripe from 'stripe'

and I want to import the types as well so I'm doing import * as Stripe from '@types/stripe'

However, I'm getting the error: Cannot import type declaration files. Consider importing 'stripe' instead of '@types/stripe'

How can I import the types so that I can use them to declare function returns, etc?

Upvotes: 11

Views: 11823

Answers (2)

cjav_dev
cjav_dev

Reputation: 3105

If you're using stripe-node [1] directly, it now supports typescript and has types defined. For those who find this and are using the types from stripe-node, you can import Stripe like the following:

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

Upvotes: 19

deerawan
deerawan

Reputation: 8443

You did correctly by installing stripe and @types/stripe. But, you don't need to import types declaration in the source file

import * as Stripe from 'stripe';

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

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

if you use VSCode or IDE editor, it should give you IntelliSense such as below

enter image description here

Hope it helps

Upvotes: 3

Related Questions