- Jul 17, 2026
- admin
- 0
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.
- 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
- 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
- Boolean
Represents either true or false.
let isLoggedIn: boolean = true;
let isCompleted: boolean = false;
console.log(isLoggedIn);
console.log(isCompleted);
- Undefined
A variable declared but not assigned a value.
let address: undefined = undefined;
console.log(address);
Output
undefined
- Null
Represents an intentional empty value.
let manager: null = null;
console.log(manager);
Output
null
- 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
- 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 |
- Void
Used for functions that don’t return anything.
function display(): void {
console.log(“Welcome”);
}
- Never
Represents functions that never finish normally.
Example 1
function throwError(): never {
throw new Error(“Invalid User”);
}
Example 2
function infiniteLoop(): never {
while(true){
}
}
- Object
Represents non-primitive values.
let employee: object = {
id:101,
name:”Anand”,
salary:50000
};
console.log(employee);
- 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]);
- Tuple
Stores multiple values of different data types.
let employee:[number,string,boolean]=
[101,”Anand”,true];
console.log(employee);
Output
101
Anand
true
- 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
- BigInt
Used for very large integers.
let population: bigint = 123456789123456789123456789n;
console.log(population);
- 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.

