TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸

Globals in TS

April 27, 2021

Old way

create a foo.d.ts file and declare a var

code
// Global file
var ENVIRONMENT = '@ConfigurationManager.AppSettings["Environment"]'; // picking up the env value from a Web.config file in .NET MVC project
code
// globalConstants.d.ts
declare var ENVIRONMENT: string;
code
// foo.ts
const Sentry = {
  dsn: "https://iamawesome.sentry.dsn",
  logger: "Awesome Web App",
  environment: ENVIRONMENT, // this is defined globally
};

New way

Use globalThis to access any globally defined variables (no need to create type declarations). read more

You must use var (no let or const)

code
// Global file
var ENVIRONMENT = '@ConfigurationManager.AppSettings["Environment"]'; // picking up the env value from a Web.config file in .NET MVC project
code
// foo.ts
const Sentry = {
  dsn: "https://iamawesome.sentry.dsn",
  logger: "Awesome Web App",
  environment: globalThis.ENVIRONMENT, // this is defined globally
};