stripe-node icon indicating copy to clipboard operation
stripe-node copied to clipboard

Feature requests: Typescript definitions

Open ronzeidman opened this issue 9 years ago • 59 comments

Hi, Since typescript popularity is on the rise and there are no community definitions available for stripe-node I would very much like Typescript definitions. This will help both Typescript users and normal JS users with IDEs that support them like WebStorm and VSCode (even Sublime and Atom have JS hinting plugins that support them). I find using type definitions much easier than going through the documentation. In addition not all the documentation is up to date since I see the functions can optionally return promises if a callback is not passed and it's not documented.

ronzeidman avatar Jan 22 '17 11:01 ronzeidman

There is an existing stripe definition in DefinitelyTyped, which is creating conflicts.

Type definitions could be added to this library directly to avoid issues.

bryanerayner avatar Feb 03 '17 14:02 bryanerayner

It's a bad type definition. It's outdated, lacking and named wrong (since it's a definition for the UI it should have been called "stripe.js" and not "stripe" which is the server side module. Also for the client side I've made a small repo: https://github.com/ronzeidman/stripe-client-ts the server side definitions are more important and longer so I think it would benefit everyone if stripe were maintaining them themselves. Also there is an es6 refactor request issue that refactoring in typescript can solve that would be even better than just including and maintaining definitions.

ronzeidman avatar Feb 04 '17 05:02 ronzeidman

I think the best solution (which would avoid the @types issue completely) would be to include the .d.ts files in this repo. They would then be included via the "types" setting in the package.json.

How much interest would there be in supporting TypeScript?

bryanerayner avatar Feb 08 '17 15:02 bryanerayner

At least 4 according to the current thumbs up 😄 Anyone interested please thumb up this issue! I suggest a twitter survey for stripe users.

ronzeidman avatar Feb 08 '17 16:02 ronzeidman

I got this working (intellisense in VScode also works from this)

npm install typings --global typings init typings install dt~stripe-node --save --global

That's all you need to do to get it working. :)

rachaeldawn avatar May 14 '17 05:05 rachaeldawn

@jschmold Could you share a little more about how you got that working? Specifically, how exactly do you import stripe to get intellisense in VScode to play nicely with it?

RP-3 avatar May 21 '17 21:05 RP-3

@RP-3

Follow all the instructions I have above, and then pretend it came with the type defs. It works automatically because it looks into the global type definitions for stripe, then intellisense just uses them

rachaeldawn avatar May 21 '17 22:05 rachaeldawn

@jschmold Sorry, I'm probably being really thick here. What exactly do you mean "pretend it came with type defs"?. I've followed your instructions above and I've tried the following to import stripe:

import * as Stripe from 'stripe'; //fails with "Could not find a declaration file for module 'stripe'"
import Stripe = require('stripe'); //fails with "Could not find a declaration file for module 'stripe'"
import * as Stripe from 'stripe-node'; //fails with "Cannot find module 'stripe-node'"
import Stripe = require('stripe-node'); //fails with "Cannot find module 'stripe-node'"
const Stripe = require('stripe'); //works but without intellisense

I've also tried the above four combinations after running npm install @types/stripe-node. The errors are the same except that:

import Stripe = require('stripe-node'); //fails with "File '.../node_modules/@types/stripe-node/index.d.ts' is not a module."
import * as Stripe from 'stripe-node'; //also fails with "File '.../node_modules/@types/stripe-node/index.d.ts' is not a module."

It seems to me that there are two things going on here. I have a vanilla node module called stripe sans definitions, and a set of typings called stripe-node. What kind of import statement can I use to import the node module stripe and have it use the definitions from stripe-node ?

RP-3 avatar May 21 '17 23:05 RP-3

@RP-3 That's really weird. That exact code works for me. Email me so we can fix it, then post the results here? I'm off of work in 4 hours, and I'll do what I can to help.

[email protected]

rachaeldawn avatar May 22 '17 00:05 rachaeldawn

@jschmold Thanks for all the help. I've just got something to work... it might be that we're using different versions of tsc ? For what its worth, I'm on 2.3.2.

For anyone else stuck with my dilemma, the following worked: npm i --save @types/stripe-node

Then import using:

import 'stripe-node'; // make stripe-node types globally visible in the current file
const Stripe: StripeNode.StripeExport = require('stripe'); // manually assign those types to the lib

Reading the section titled (Import a module for side-effects only)[https://www.typescriptlang.org/docs/handbook/modules.html] I guessed @types/stripe-node is written to be imported as a global module and won't work otherwise. Assuming that's right, it makes sense to me that I wouldn't be able to import both the lib and the differently-named definitions in a single statement.

UPDATE: This also comes with a horrible hack. import 'stripe-node'; will compile to require('stripe-node'), which will then fail at runtime. One god-awful way to fix this for me was to add the following two lines to my package.json:

"postinstall": "npm run compile",
"compile": "tsc && cp -r node_modules/@types/stripe-node node_modules/stripe-node/ && echo \"module.exports = {}\" >> node_modules/stripe-node/index.js",

Frankly, I hate this solution so if anyone knows of anything better please comment and let me know.

RP-3 avatar May 22 '17 00:05 RP-3

@RP-3 , if you got it to work that's great. I hate this too, but you have just made it possible to actually use this thing.

bryanerayner avatar May 22 '17 13:05 bryanerayner

Any word from a maintainer of this repo as to whether or not a .d.ts file could be included directly in this module?

bryanerayner avatar May 22 '17 13:05 bryanerayner

I found out what made it work with minimal effort for me.

Before you start, if you don't have it already npm install typings --global

To get stripe's intellisense working npm init npm install stripe --save typings install dt~stripe-node --save --global touch tsconfig.json

The TSConfig

{
    "compileOnSave": true,
    "compilerOptions": {
        "allowJs": true
    }
}

The moment the TSConfig exists, my intellisense works with no effort, even if I am using a js file.

intellisense working

rachaeldawn avatar May 22 '17 14:05 rachaeldawn

I followed @jschmold and got it working too. Thanks!

manu-st avatar May 24 '17 22:05 manu-st

@manu-silicon You're welcome :) I use intellisense to learn APIs and such quickly, so not having it makes me feel blind. Combining with Typescript also helps, so I wanted to find a solution

rachaeldawn avatar May 24 '17 23:05 rachaeldawn

Any updates on whether or not you're planning to add type definitions? Doing so would be greatly appreciated.

@jschmold Nice workaround you've found there! However, to me it doesn't seem like a solution to use a hack, since every notable library out there provides type definitions already.

Thanks!

rbluethl avatar Jun 13 '17 17:06 rbluethl

Following @RP-3 initial suggestion worked well.

I just created a file called stripe.ts:

import 'stripe-node';
const Stripe: StripeNode.StripeExport = require('stripe');
export { Stripe };

Then I can import like normal:

import { Stripe } from './stripe';

I do this with most external dependencies so I can hack the import and fix typings when needed.

At least this will work until an official solution is found.

Update:

Nevermind, this blows up when I try to use webpack (and fails at runtime as @RP-3 said)...

Looks like I will use it while coding and then comment out the type information for runtime (making the Stripe any).

ricklove avatar Jul 13 '17 04:07 ricklove

For anyone struggling with stripe and using the types, within TypeScript (or ES6+), what has worked for me across both Flow and TypeScript is

Install the types

npm install --dev @types/stripe-node
or
yarn add @types/stripe-node -D

Import the package and use the exported constructor

import * as Stripe from 'stripe';
const stripe = new Stripe(stripeKey);

// Then do whatever you like:
const customer: Promise<StripeNode.customers.ICustomer> = stripe.customers.create( ... )

All the types exist under the StripeNode module namespace, because the @types/stripe-node index.d.ts has a top-level declare namespace StripeNode { ... }. So when you want to specify a type on a function parameter or a variable, then you will use StripeNode.Foo.

The @types/stripe-node index.d.ts also declares a module called stripe with a non-default export of an interface that contains a constructor. So when you import * as Stripe from 'stripe', you'll get that interface, with the constructor. Invoking the constructor will return a stripe instance with all the methods you expect to see typings and autocomplete for.

sampsonjoliver avatar Oct 09 '17 22:10 sampsonjoliver

What is the current status on this issue? It has nearly been a year since @ronzeidman opened it and the issue is relatively easy to fix. Is this not going to have a resolution? If not, let the community know and close the issue. It seems like maintainers have just dropped back and not given a clear direction on what's going on concerning this.

iamclaytonray avatar Dec 13 '17 22:12 iamclaytonray

The current typings on DefinitelyTyped are pretty old and unfortunately, I am unable to augment them easily. Cannot augment module 'stripe' because it resolves to a non-module entity.

We need better types for such a critical piece of software :-)

karthikiyengar avatar Jan 11 '18 19:01 karthikiyengar