How to Declare Variables in TypeScript ?
Last Updated :
09 Jul, 2024
In TypeScript, a variable can be defined by specifying the data type of the value it is going to store. It will store the values of the specified data type only and throws an error if you try to store the value of any other data type. You can use the colon syntax(:) to declare a typed variable in TypeScript. You need to declare variables in this format because TypeScript is a statically typed language not a loosely typed language that is why you need to explicitly type the variables before assigning them values of a particular type.
Syntax:
const variable_name: data_type = value;
Example: The below code defines a variable with specified data types in TypeScript.
JavaScript
const var1: string = "GFG";
const var2: number = 35;
console.log(var1);
console.log(var2);
Output:
"GFG"
35
Declaring Object Variables with Type Annotations
In TypeScript, you can also define the structure of an object by specifying the data type for each of its properties. This ensures that each property adheres to the declared type, and attempting to assign a value of an incorrect type will result in a compile-time error.
Syntax:
const variableName: { property1: dataType1; property2: dataType2; ... } = {
property1: value1,
property2: value2,
...
};
Example:
In this example, we'll define a Book
object with properties for title, author, and publication year, each with specific types.
JavaScript
const book: { title: string; author: string; year: number } = {
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
year: 1925
};
console.log(book.title); // Output: "The Great Gatsby"
console.log(book.year); // Output: 1925
Purpose: This example illustrates how TypeScript allows for detailed structuring and typing of objects, which is crucial in larger, more complex applications where ensuring data integrity is essential. It helps developers prevent bugs related to incorrect data types, which can be a common issue in dynamically typed languages like JavaScript.
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 Create a Global Variable in TypeScript ? 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: Table of Content Using var, let or const at the Top LevelUsing window
3 min read
Variables in TypeScript In TypeScript, variables are used to store values that can be referenced and manipulated throughout your code. TypeScript offers three main ways to declare variables: let, const, and var. Each has different behavior when it comes to reassigning values and scoping, allowing us to write more reliable
6 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
How to declare a module in TypeScript ? A module is a piece of code that can be called or used in another code. There is nothing new about modules in Typescript. The concept of the module was introduced by JavaScript with ECMAScript 2015 release. Typescript is just re-using this feature.Will the code not work without Modules?Of course, it
7 min read