Feature requests: Typescript definitions
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.
There is an existing stripe definition in DefinitelyTyped, which is creating conflicts.
Type definitions could be added to this library directly to avoid issues.
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.
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?
At least 4 according to the current thumbs up 😄 Anyone interested please thumb up this issue! I suggest a twitter survey for stripe users.
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. :)
@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
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
@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 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.
@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 , 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.
Any word from a maintainer of this repo as to whether or not a .d.ts file could be included directly in this module?
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.
I followed @jschmold and got it working too. Thanks!
@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
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!
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).
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.
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.
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 :-)