How to Create a Global Variable in TypeScript ?
Last Updated :
28 Apr, 2025
To create a global variable in TypeScript, we have multiple approaches. In this article, we are going to learn how to create a global variable in TypeScript.
Below are the approaches used to create a global variable in TypeScript:
Using var, let
or const
at the Top Level
Declare a variable with var, let
or const
at the top level of your script.
Example: In this example, we are using var, let, and const to create a global variable in TypeScript.
JavaScript
var globalVar: string = "I am a global variable using var";
let globalLet: string = "I am a global variable using let";
const globalConst: number = 42;
function useGlobalVarLetAndConst() {
console.log(globalLet, globalConst, globalVar);
}
useGlobalVarLetAndConst(); // Accessible here
Output:
I am a global variable using let
42
I am a global variable using var
Using window
Object in Browser Environments
Assign a property to the window
object for global accessibility in browsers.
Example: In this example, we are using window
Object in Browser Environments to create a global variable in TypeScript.
JavaScript
// Using the window object in browser environments
(window as any).browserGlobal =
"I am a global variable accessible in browsers";
function useBrowserGlobal() {
console.log((window as any).browserGlobal);
}
useBrowserGlobal(); // Accessible here (in browser environments)
Output:
I am a global variable accessible in browsers
Using a Namespace or Module
Place the variable inside a namespace or module for organized global scope.
Example: In this example, we are using a Namespace or Module to create a global variable in TypeScript.
JavaScript
// Using a namespace or module
namespace GlobalNamespace {
export let globalVariable: string = "I am a global variable";
}
function useGlobalNamespace() {
console.log(GlobalNamespace.globalVariable);
}
useGlobalNamespace(); // Accessible here
Output:
I am a global variable
Using declare
Keyword
Use the declare
keyword for external global variables without a specific implementation.
Example: In this example, we are using declare
Keyword to create a global variable in TypeScript.
JavaScript
// Using declare keyword
declare var declaredGlobal: boolean;
function useDeclaredGlobal() {
console.log(declaredGlobal);
}
useDeclaredGlobal(); // Accessible here
Output:
The output will depend on the actual value assigned to externalLibraryVar
at runtime. For example, if it is defined externally before calling useExternalVariable()
, the output might look like this:
[declaredGlobal value]
If declaredGlobal is not defined, you might encounter a runtime error similar to:
Uncaught ReferenceError: declaredGlobal is not defined
Similar Reads
How to Get a Variable Type in TypeScript Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific
2 min read
How to write a function in Typescript ? Writing a function in TypeScript is similar to writing it in JavaScript but with added parameters and return type. Note that any JavaScript function is a perfectly valid TypeScript function. However, we can do better by adding type.Syntax: Let's see a basic TypeScript function syntax (with two argum
4 min read
How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let
4 min read
How to Access Enum Values in TypeScript ? Enums are a feature in TypeScript that help organize collections of related values. Accessing these values efficiently is crucial for clean, maintainable code. This guide provides a straightforward overview of different methods to access enum values in TypeScript, including bracket notation, dot not
3 min read
TypeScript Type Annotations on Variables TypeScript is a statically typed superset of JavaScript that brings the benefits of strong typing to the JavaScript ecosystem. One of its key features is the ability to add type annotations to variables, which helps developers catch type-related errors at compile time rather than runtime. In this ar
3 min read
How to Create Nested Classes in TypeScript ? In TypeScript, you can create nested classes using different methods. We will discuss about three different approaches to creating nested classes in TypeScript. These are the approaches: Table of Content By defining nested classes inside a classBy using the namespacesBy using the modulesBy defining
3 min read