Data Types in TypeScript

1 min read

TypeScript provides several data types to work with. Here are the main types:
# Primitive Types
1. **Boolean**
```typescript
let isDone: boolean = false;
Number
let decimal: number = 6; let hex: number = 0xf00d; let binary: number = 0b1010; let octal: number = 0o744;
String
let color: string = "blue"; color = "red";
Array
let list: number[] = [1, 2, 3]; let list: Array<number> = [1, 2, 3];
Tuple
let x: [string, number]; x = ["hello", 10];
Enum
enum Color { Red, Green, Blue, } let c: Color = Color.Green;
Any
let notSure: any = 4; notSure = "maybe a string instead"; notSure = false;
Void
function warnUser(): void { console.log("This is my warning message"); }
Null and Undefined
let u: undefined = undefined; let n: null = null;
Never
function error(message: string): never { throw new Error(message); }
Object
let obj: object = { name: "John", age: 30 };
These types help in building robust and type-safe applications in TypeScript.
0
Subscribe to my newsletter
Read articles from Soumya Ranjan Mahanta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
