I usually have some server-side variables that I put into the global scope, so that I can access them from anywhere in my client side TypeScript code, e.g.:
Server in .cshtml:
<script>
const RazorGlobals = {
apiBaseUrl: '@Model.ApiBaseUrl'
}
</script>
Client code:
declare const RazorGlobals: {
apiBaseUrl: string
}
fetch(RazorGlobals.apiBaseUrl + '/myResource').then(doStuff)
At the moment I have to repeat the declare const RazorGlobals...
part at the top of every file where I need to use it. I am looking for a way to define this declaration in a single place, something like this pseudocode:
globals.d.ts:
export declare const RazorGlobals: {
apiBaseUrl: string
}
file1.ts:
import './globals'
fetch(RazorGlobals.apiBaseUrl + '/myResource').then(doStuff)
file2.ts:
import './globals'
fetch(RazorGlobals.apiBaseUrl + '/myOtherResource').then(doOtherStuff)
Is there a way to achieve something like this?