How to Declare Variables in TypeScript ? Last Updated : 09 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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"35Declaring Object Variables with Type AnnotationsIn 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. Comment More infoAdvertise with us Next Article How to Declare Variables in TypeScript ? A abhish8rzd Follow Improve Article Tags : JavaScript Web Technologies JavaScript-QnA WebTech-FAQs 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 How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod 2 min read How to get Value from a Dictionary in Typescript ? In TypeScript, a dictionary consists of objects that store data in the key-value pairs, and allow retrieval of values based on the specified keys. We can get value from a dictionary for the specific keys using various approaches. Table of Content Using Dot NotationUsing Bracket NotationUsing a Funct 2 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 Declare an Empty Array in TypeScript? TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec 2 min read Like