Jithu P Rajan Jithu P Rajan - 29 days ago 37
TypeScript Question

Environment variables in TypeScript

If we have some environment variables which are present in the environment in which the compiled JavaScript is going to run, how we can mention those variables in Typescript code ?

In TypeScript varaibles cannot be used without being declared. If it is declared like

let env_varaible;
, it become
var env_variable;
which eventually overridethe actual varaible. What is the solution for this?

Answer

You can tell TypeScript about a variable created by globals (coming directly from JavaScript runtime environment or any external source) with:

declare var env_variable;

Typescript won't throw an error for undeclared env_variable variable.

See also: declare global namespace variable from TypeScript