@types/xxxx
package and had problems.Definitions by:
in index.d.ts
) so they can respond.I'm using this line in order to import @types/stripe
:
import * as Stripe from '@types/stripe';
I've also tried with:
import * as Stripe from 'stripe';
I'm getting the message:
[ts] File 'd:/projects/living/user-platform/project/node_modules/@types/stripe/index.d.ts' is not a module.
I'm using typescript 2.2.1.
Any ideas?
I'm struggling with the same issue. Are you trying to use stripe type definitions client-side or server-side?
If server-side, the correct type package is actually "stripe-node", not "stripe", which is really the typings for Stripe.Js.
However, I still haven't gotten the stripe-node typings to work. VSCode says that "index.d.ts" is not a module. But it looks like one when I review the file.
Think I've figured it out.
Short version: you don't need an import statement for the stripe-node package. You can just do this:
let foo = Stripe.card;
And you will get intellisense options for the object.
Longer version: the reason for this is that stripe-node module does not export a module; it looks like it tries, but it doesn't seem to work. Perhaps typescript changed since they created the typing?
Either way, when a type is not included in a module it is considered a global type. Lucky for us, TypeScript automatically includes global types.
By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.
Thanks @jelling. I'm trying to use it on client side. According to this question on stackoverflow and this issue it seems @types/stripe
is outdated.
So the only solution is to add <script>
tag reference on index.html
and import * as Stripe from 'stripe';
into your typescript code.
@jeusdi @jelling Did you solve this issue, I've used import * as Stripe from 'stripe'; and it's flagging that it can't find stripe. I installed stripe client side - version 4.18.
@stephenstroud Here's my client-side code that works for me:
var handler = (<any>window).StripeCheckout.configure({
key: '{yourkey}',
locale: 'auto',
allowRememberMe: false,
email: email,
token: function (token: any) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
console.log(token);
onboard.checkout(token)
.then((result)=>
// TODO: put whatever you want to happen after checkout
);
}
});
handler.open({
name: 'XXX',
description: 'YYYY',
amount: 49900
});
Because Stripe has no working type file at this point, we need to tell Typescript that the object exists. I do that in the first line via this code:
(<any>window).StripeCheckout.configure({
The "
Alternately, the following will also work and look prettier:
declare var StripeCheckout: any; // ambient declaration
@jelling Thanks so much for this, it has saved me a huge headache and works well. I went with (
You don't have to import anything. If you have older TypeScript you probably need to include it in tsconfig.json in types
section only.
There are now 2 packages @types/stripe-v3
and @types/stripe-v2
, so depeneding which version You use, or both you should include those. Intelisense should be out of box, as Stripe is global object.
I think this issue should be closed.
I use StripeCheckout
too.
But I get error message.
~~~
ERROR in [at-loader] ./node_modules/@types/stripe-checkout/index.d.ts:6:23
TS2688: Cannot find type definition file for 'stripe/v2'.
ERROR in [at-loader] ./node_modules/@types/stripe-checkout/index.d.ts:19:21
TS2304: Cannot find name 'StripeTokenResponse'.
~~~
I added @types/stripe-checkout
and @types/stripe-v2
.
What this?
@ncaq sounds need to be fixed. I will send Pull Request.
@ncaq I've submitted Pull Request here:
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18243
Sorry for trouble.
@galuszkak Does that PR fix this issue? Good to close?
@andy-ms yes.
Im getting this same error on v3.0.7 using angular 6 any advice or solution?
@bitgandtter Check the installed version of @types/stripe
?
@bitgandtter You should use @types/stripe-v2
or @types/stripe-v3
not @types/stripe
depend on which version You are using. ;)
im using @types/stripe-v3 version 3.0.7
quite long time without answer... but I'm still facing this issue. I'm using @types/stripe-v3.
@bitgandtter did you mange to solve that?
I am using v3.0.8 with Angular 6.1.10 and Angular CLI v6.2.5 with Typescript and I have the same problem.
My IntelliJ IDE finds the types and wants to import objects in this unusual fashion:
import Source = stripe.Source.
but that gives me the error: Cannot find namespace 'stripe';
It also cannot find 'Stripe' object to import.
I have tried all the different ways to import Stripe the way I import my other Typescript types but it doesn't want to be found.
E.g.
import {Source} from 'stripe-v3';
import {Source} from 'stripe-v3/Source';
import * as stripe from 'stripe-v3';
I had to revert back to declaring Stripe:any and stripe:any in my typings file but that's not something I really want to do.
At this point, I don't know what the @types/stripe-v3 library is good for because I cannot figure out how to use it. This has not been an issue with any other types I've used before unless the developer forgot to explicitly export / declare the classes.
The way the package @types/stripe-v3
is declared now is that it declares a global variable Stripe
and a global namespace stripe
containing interfaces. You don't import it at all but just start using the Stripe
variable directly.
Tried following your advice and not import anything. My IDE cannot find the variables. Angular CLI complains as well.
Could you attach a a working sample?
@bjornharvold Try /// <reference types="stripe-v3" />
to ensure the types are included in the project.
Thanks @andy-ms - For everyone else coming across this thread who is having issues getting Stripe types to work with Angular and Webpack:
npm i --save-dev @types/stripe-v3
Edit your tsconfig.app.json file and add:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/app",
"module": "es2015",
"types": [
"stripe-v3"
]
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
That's it. Now you can refer to Stripe such as this:
const stripe: stripe.Stripe = Stripe('key');
Yeah, adding the type in the tsconfig.app.json works ! Thanks @bjornharvold
Add typeRoots for your tsconfig.json:
{
"compilerOptions": {
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
And make sure you remove types: [] from your tsconfig.app.json or set the correct ones.
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
// "types": [] <-- Remove this line to just use all @types
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
Because:
If types is specified, only packages listed will be included.
From: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Or set them explicit:
{
"compilerOptions": {
"types" : ["node", "lodash", "express"]
}
}
ERROR in node_modules/ngx-stripe/lib/services/stripe.service.d.ts(20,24): error TS1144: '{' or ';' expected.
Please help me ti fix this bug. thanks
Most helpful comment
Thanks @andy-ms - For everyone else coming across this thread who is having issues getting Stripe types to work with Angular and Webpack:
npm i --save-dev @types/stripe-v3
Edit your tsconfig.app.json file and add:
That's it. Now you can refer to Stripe such as this:
const stripe: stripe.Stripe = Stripe('key');