AnyCompiler

Modules aur Standard Library

TypeScript code aksar multiple files mein split hota hai, import aur export se connected — wahi syntax jo modern JavaScript use karta hai. Yeh compiler ek time mein ek hi file run karta hai, isliye multi-file imports yahan directly demonstrate nahi kiye ja sakte, lekin is lesson mein baaki sab kuch bilkul waise hi run hota hai jaise dikhaya gaya hai.

Utility types — existing types se naye types banana

TypeScript kuch built-in utility types ke sath aata hai jo ek existing type ko transform karte hain. Partial<T> T ki har property ko optional bana deta hai — kisi update function jaisi cheez ke liye useful hai jahan aap sirf woh fields dete ho jo change ho rahe hain.

interface Product {
  id: number;
  name: string;
  price: number;
}

function updateProduct(product: Product, changes: Partial<Product>): Product {
  return { ...product, ...changes };
}

const original: Product = { id: 1, name: "Keyboard", price: 50 };
const updated = updateProduct(original, { price: 45 });
console.log(updated);
Output
{ id: 1, name: 'Keyboard', price: 45 }

Pick<T, Keys> T se sirf listed properties ke sath ek naya type banata hai; Record<Keys, Type> ek object type banata hai jahan Keys ki har key same value type ko map karti hai. Dono hi ek naya, precise type ek aise type se derive karne ke tarike hain jise aap pehle se define kar chuke ho, use haath se likhne ke bajaye.

interface Product {
  id: number;
  name: string;
  price: number;
}

type ProductSummary = Pick<Product, "id" | "name">;

const summary: ProductSummary = { id: 1, name: "Keyboard" };
console.log(summary);
Output
{ id: 1, name: 'Keyboard' }

Bas itna hi hai JavaScript ke upar TypeScript ke type system ka core. Yahan se, best next step hai practice — koi chhota sa idea pick karo aur use banao, types par depend karte hue jo raaste mein mistakes pakdein.

← Type NarrowingAur lessons jald aa rahe hain