Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 & Stripe error TS2304: Cannot find name 'Stripe'

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'.
like image 733
Robin De Schepper Avatar asked Feb 18 '19 18:02

Robin De Schepper


2 Answers

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.

like image 50
Robin De Schepper Avatar answered Oct 05 '22 22:10

Robin De Schepper


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.

like image 28
Krishna Pravin Avatar answered Oct 05 '22 22:10

Krishna Pravin