DataTypes in Typescript



Data Types in TypeScript

TypeScript is a strongly typed superset of JavaScript, which means every variable can have a specific data type. This helps catch errors during development and improves code readability and maintainability.

  1. Number

Represents both integers and floating-point numbers.

let age: number = 30;

let salary: number = 75000.50;

let marks: number = 95;

Output

Age: 30

Salary: 75000.5

Marks: 95

  1. String

Represents textual data.

let name: string = “Anand”;

let city: string = ‘Hyderabad’;

let course: string = `Playwright`;

String Interpolation

let trainer = “Anand”;

let technology = “Playwright”;

console.log(`${trainer} is teaching ${technology}`);

Output

Anand is teaching Playwright

  1. Boolean

Represents either true or false.

let isLoggedIn: boolean = true;

let isCompleted: boolean = false;

console.log(isLoggedIn);

console.log(isCompleted);

  1. Undefined

A variable declared but not assigned a value.

let address: undefined = undefined;

console.log(address);

Output

undefined

  1. Null

Represents an intentional empty value.

let manager: null = null;

console.log(manager);

Output

null

  1. Any

Can store any type of value.

let value: any = 100;

value = “Playwright”;

value = true;

value = [10,20,30];

console.log(value);

Advantages

  • Accepts any datatype
  • Useful for migration from JavaScript

Disadvantages

  • No type checking
  • Reduces TypeScript safety
  • Not recommended unless necessary
  1. Unknown

Similar to any, but safer.

let data: unknown = “TypeScript”;

if (typeof data === “string”) {

console.log(data.toUpperCase());

}

Output

TYPESCRIPT

Difference between Any and Unknown

any unknown
No type checking Type checking required
Unsafe Safe
Direct operations allowed Need type checking first
  1. Void

Used for functions that don’t return anything.

function display(): void {

console.log(“Welcome”);

}

  1. Never

Represents functions that never finish normally.

Example 1

function throwError(): never {

throw new Error(“Invalid User”);

}

Example 2

function infiniteLoop(): never {

while(true){

}

}

  1. Object

Represents non-primitive values.

let employee: object = {

id:101,

name:”Anand”,

salary:50000

};

console.log(employee);

  1. Array

Stores multiple values of the same type.

Method 1

let numbers:number[]=[10,20,30,40];

Method 2

let cities:Array<string>=[“Hyderabad”,”Delhi”,”Mumbai”];

Access Elements

console.log(numbers[0]);

console.log(cities[1]);

  1. Tuple

Stores multiple values of different data types.

let employee:[number,string,boolean]=

[101,”Anand”,true];

console.log(employee);

Output

101

Anand

true

  1. Enum

Represents a fixed set of constants.

enum Browser

{

Chrome,

Edge,

Firefox

}

let browser:Browser=Browser.Chrome;

console.log(browser);

Output

0

Better Example

enum Browser

{

Chrome=”chrome”,

Edge=”edge”,

Firefox=”firefox”

}

console.log(Browser.Edge);

Output

edge

  1. BigInt

Used for very large integers.

let population: bigint = 123456789123456789123456789n;

console.log(population);

  1. Symbol

Creates a unique identifier.

let id1 = Symbol(“id”);

let id2 = Symbol(“id”);

 

console.log(id1 === id2);

Output

false

Type Inference

TypeScript can automatically detect the datatype.

let age = 25;

let name = “Anand”;

let isTrainer = true;

Equivalent to

let age: number = 25;

let name: string = “Anand”;

let isTrainer: boolean = true;

Type Annotation

Explicitly specifying the datatype.

let age:number=30;

let company:string=”IBM”;

let passed:boolean=true;

Type Assertion

Tell TypeScript the datatype manually.

let value:any=”Playwright”;

let len=(value as string).length;

console.log(len);

or

let len=(<string>value).length;

Data Type Hierarchy

Data Types

├── Primitive

│   ├── number

│   ├── string

│   ├── boolean

│   ├── bigint

│   ├── symbol

│   ├── null

│   └── undefined

├── Special

│   ├── any

│   ├── unknown

│   ├── void

│   └── never

└── Non-Primitive

├── object

├── array

├── tuple

└── enum

Best Practices

  • ✅ Use explicit types for public APIs and function parameters.
  • ✅ Prefer unknown over any when handling external or dynamic data.
  • ✅ Use type inference where the type is obvious.
  • ✅ Use enums only for fixed sets of related constants.
  • ✅ Enable strict mode in tsconfig.json for stronger type checking.
  • ❌ Avoid excessive use of any, as it defeats TypeScript’s type safety.

Tags:
Leave a comment

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

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!