Angular 7 & Stripe error TS2304: Cannot find name 'Stripe'
Asked Answered
M

2

16

I've installed @types/stripe-v3 and included Stripe's javascript file in a script tag in index.html. Supposedly the Angular compiler should include all files automagically from the @types node modules. Reading up on the internet and looking at @types/stripe-v3/index.d.ts there should be a var Stripe declared globally if the file is included by the compiler. From index.d.ts

declare var Stripe: stripe.StripeStatic;

In my service file I have the following code:

import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class BetalingService {
  stripe = Stripe(environment.stripeKey);

  constructor() { }

}

Resulting in the following error:

error TS2304: Cannot find name 'Stripe'.
Mccarter answered 18/2, 2019 at 18:33 Comment(3)
how about defining import statement for Stripe?Eclecticism
If you try an import statement like import { Stripe } from 'stripe-v3'; you get the error 'stripe-v3 is not a module', because the index.d.ts file indeed does not declare a module, it is meant to declare this global var Stripe upon inclusion by the compilerMccarter
yarn add @types/stripe-v3 --saveSuricate
M
31

The issue is resolved by including a reference to the @types/stripe-v3 package in the compilerOptions.types array of your tsconfig.app.json file in the src directory of your Angular project.

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": [
      "stripe-v3"
    ]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

This solution was posted by bjornharvold in this thread.

Mccarter answered 18/2, 2019 at 18:43 Comment(5)
In my case I was using @types/stripe-checkout. I was having the same problem and adding stripe-checkout to the types array didn't work. Instead I added @types/stripe-checkout to the array which solved the problem. Hopefully this can help someone else.Foetid
npm install --save @types/stripe-v3 (thought I'd suggest the explicit for any newbies)Broomfield
@jesper exactly what i was looking for. i was digging around for hours.Milka
I have my tsconfig.base.json and tsconfig.app.json files setup as described here. That removed the red squigglies in the editor, but the actual typescript compilation still complains with the error message mentioned by OP. :(Resinate
Try to delete the types array as mentioned in the answer belowMccarter
B
8

Angular imports types based on values of compilerOptions.types and compilerOptions.typeRoots from the typescript configuration file. TS compilerOptions docs reference

By default Angular projects created using angular cli will have two typescript configuration files.

  1. tsconfig.json in the root of the project
  2. tsconfig.app.json in /src directory

If both types and typeRoots are undefined angular will import all the typings from node_modules/@types/*.

But if any one of them have any value, only the specified types or the types from the specified location will be imported. E.g: types: ['stripe-v3'] or typeRoots: ['/someDir']. So all other installed types in node_modules/@types/* will not be imported.

If an empty array is set, then no types will be imported automatically from node_modules/@types. types: [] or typeRoots: [].

By default compilerOptions.types in tsconfig.app.json will have an empty array as its value. This is the reason why angular does not get the installed typings from node_modules/@types/* automatically.

To fix this: npm install @types/stripe-v3 the typings and in tsconfig.app.json

  1. Either add stripe-v3 to the types.
...
"compilerOptions": {
   ...
  "types": ['stripe-v3']
}
  1. Or delete types from compilerOptions

If you add, you will have to add all future typings to this array.

Instead if you remove types from compilerOptions angular will import all future typings automatically.

Also make sure to check types and typeRoots in tsconfig.js also. typeRoots will have relative paths as array and the same logic applies here as well.

Beanpole answered 6/9, 2019 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.