Chapter 3TypeScript~1 min read
Arrays, Objects आणि Tuples
Complex Data Types
TypeScript मध्ये arrays, objects आणि tuples strongly typed असतात — array मध्ये फक्त define केलेल्या type च्या values जाऊ शकतात.
Arrays
typescript
// Number array
let scores: number[] = [90, 85, 92];
// किंवा
let scores2: Array<number> = [90, 85, 92];
// String array
let cities: string[] = ["Pune", "Mumbai", "Nashik"];
// Mixed array (union type)
let mixed: (string | number)[] = ["hello", 42, "world"];
// Array methods typed आहेत
scores.push(88); // ✅
scores.push("A"); // ❌ ERRORObjects — type annotation
typescript
// Inline object type
let student: { name: string; age: number; isPremium: boolean } = {
name: "Avadhoot",
age: 22,
isPremium: true,
};
// Optional property (? लावा)
let user: { name: string; email?: string } = {
name: "Rahul",
// email नसलं तरी चालतं
};Tuples — fixed-length typed arrays
typescript
// Tuple: specific positions ला specific types
let coordinate: [number, number] = [18.5, 73.8]; // [lat, lng]
let userInfo: [string, number, boolean] = ["Avadhoot", 22, true];
// Destructuring
const [lat, lng] = coordinate;
// Named tuples (readability साठी)
let point: [x: number, y: number] = [10, 20];✅ Key Points — लक्षात ठेवा
- ▸number[] किंवा Array<number> — typed array
- ▸Optional property: property?: type
- ▸Tuple: fixed positions, fixed types
- ▸Union array: (string | number)[]
0/9 chapters पूर्ण