Access or declare values in the global scope in TypeScript
create a foo.d.ts file and declare a var
1// Global file
2var ENVIRONMENT = '@ConfigurationManager.AppSettings["Environment"]'; // picking up the env value from a Web.config file in .NET MVC project
1// globalConstants.d.ts
2declare var ENVIRONMENT: string
1// foo.ts
2const Sentry = {
3 dsn: "https://iamawesome.sentry.dsn",
4 logger: "Awesome Web App",
5 environment: ENVIRONMENT, // this is defined globally
6 },
Use globalThis to access any globally defined variables (no need to create type declarations). read more
You must use var (no let or const)
1// Global file
2var ENVIRONMENT = '@ConfigurationManager.AppSettings["Environment"]'; // picking up the env value from a Web.config file in .NET MVC project
1// foo.ts
2const Sentry = {
3 dsn: "https://iamawesome.sentry.dsn",
4 logger: "Awesome Web App",
5 environment: globalThis.ENVIRONMENT, // this is defined globally
6 },