Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
Vikram Khemlani Avatar asked Dec 11 '22 01:12

Vikram Khemlani


2 Answers

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

like image 155
cjav_dev Avatar answered Jan 31 '23 05:01

cjav_dev


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

like image 37
deerawan Avatar answered Jan 31 '23 03:01

deerawan