Angular 7 & Stripe 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
- Either add
stripe-v3
to thetypes
.
...
"compilerOptions": {
...
"types": ['stripe-v3']
}
- 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.