NashTech Insights

TypeScript Data Types

Alka Vats
Alka Vats
Table of Contents

Introduction:

In TypeScript, data types play a crucial role in defining the nature of variables, parameters, and return values within the code. By explicitly specifying data types, developers can catch errors during development and enhance the overall reliability and maintainability of their applications. In this blog post, we will explore the different data types available in TypeScript, providing detailed explanations and examples to demonstrate their usage and benefits.

If you want to learn about the comparison between typescript and javascript, you can refer here.

Primitive Types:

TypeScript includes several primitive data types similar to JavaScript:

a) number: Represents numeric values, including integers and floating-point numbers. For example:

let age: number = 25;
let pi: number = 3.14;

b) string: Represents textual data enclosed in single quotes (”) or double quotes (“”). For example:

let name: string = "John Doe";
let message: string = 'Hello, TypeScript!';

c) boolean: Represents a logical value of either true or false. For example:

let isLogged: boolean = true;
let hasPermission: boolean = false;

d) null and undefined: Represents the absence of a value. For example:

let data: null = null;
let value: undefined = undefined;

e) symbol: Represents a unique identifier. Symbols are often used as keys in objects. For example:

const id: symbol = Symbol("uniqueID");

Arrays:

Arrays in TypeScript allow you to store multiple values of the same type. You can define an array using the type followed by square brackets ([]). For example:

let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["John", "Jane", "Bob"];

You can also use the generic array type Array<type> to define arrays. For example:

let cities: Array<string> = ["New York", "London", "Paris"];

Objects:

Objects allow you to store key-value pairs. In TypeScript, you can define the shape of an object using interfaces or type aliases. For example:

interface Person {
  name: string;
  age: number;
}

let person: Person = {
  name: "John",
  age: 30,
};

Alternatively, you can use type aliases to define object types:

type Point = {
  x: number;
  y: number;
};

let origin: Point = {
  x: 0,
  y: 0,
};

Enums:

Enums provide a way to define a set of named constants. This can be useful when working with a limited set of values. For example:

enum Color {
  Red,
  Green,
  Blue,
}

let primaryColor: Color = Color.Red;

In this example, the Color enum defines three values: Red, Green, and Blue. The variable primaryColor is assigned the value Color.Red.

Custom Types:

TypeScript allows you to create custom types using the type keyword or interfaces. This helps define complex data structures. For example:

type Employee = {
  name: string;
  age: number;
  department: string;
};

let employee: Employee = {
  name: "John Doe",
  age: 35,
  department: "Sales",
};

or

interface Car {
  brand: string;
  model: string;
  year: number;
}

let myCar: Car = {
  brand: "Toyota",
  model: "Camry",
  year: 2022,
};

Conclusion:

Understanding TypeScript data types is essential for writing reliable and maintainable code. By leveraging the various data types available, developers can catch errors early in the development process and improve the overall quality of their applications. Whether it’s primitive types, arrays, objects, enums, or custom types, TypeScript provides a robust type system that enhances the development experience and helps build more robust and scalable applications.

Finally, for more such posts, please follow our LinkedIn page- FrontEnd Competency.

Alka Vats

Alka Vats

Alka Vats is a Software Consultant at Nashtech. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: