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'.
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.
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.
tsconfig.json in the root of the projecttsconfig.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
stripe-v3 to the types....
"compilerOptions": {
...
"types": ['stripe-v3']
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With