I have a line of code in my Next JS application that uses a variable from a script src in the app.tsx page like so
app.tsx:
<script src="https://js.stripe.com/v3/"></script>
config.ts:
declare var Stripe:any
const stripe = Stripe("stripe publishable key")
This works because TS doesn't recognize the variable Stripe, but at build time it's loaded in from the script and it can be initializes. The declare statement bypasses the error without giving the variable a previous value. But when the TypeScript is compiled to JS with npm run build, the declare statement disapears and I get an error before the build completes. So, are there any other ways I could bypass the error similare to a declare statement, or maybe a way of better importing the script?
this work fine becasuse the type "any" includes function type,and typescript is working before build,you should know typescript is a tool for convenience during development. the result of error is Stripe is a undefined varible,undefined variable can't be execute. so when execute the code,it will get error.so build tools can detect it,it nothing to do typescript. the right way is:
declare var Stripe:(value: string) => {...}
const stripe = Stripe("stripe publishable key")