Oldingi darslarda biz object typelarni aniq yozishni ko‘rdik. Masalan:

let user: { name: string age: number }

Bu degani:

object ichida aynan shu propertylar bo‘lishi kerak

📌 Muammo

Ba'zan object ichidagi property nomlari oldindan aniq bo‘lmaydi. Masalan:

let scores= { math: 95, english: 88, physics: 92 }

Bu yerda fanlar soni o‘zgarishi mumkin. Masalan keyin:

biology history chemistry

qo‘shilishi mumkin. Shuning uchun oldindan:

math english physics

deb yozib qo‘yish qiyin.

📌 Record Type nima?

TypeScript da bunday holat uchun Record type bor. Syntax:

Record <KeyType,ValueType>

📌 Misol

let scores: Record <string,number>

Bu degani:

key → string value → number

📌 Misol

let scores: Record<string,number> = { math:95, english:88, physics:92 }

Bu to‘g‘ri ishlaydi.

❌ Noto‘g‘ri misol

letscores:Record<string,number>= { math:95, english:"good" }

TypeScript error beradi. Chunki:

value → number bo‘lishi kerak

📌 Record qachon ishlatiladi?

Record ishlatiladi agar:

object property nomlari oldindan aniq bo‘lmasa

Masalan:

  • API response
  • dynamic data
  • dictionary object
  • key-value data

📌 Record vs Object Type

Oddiy object type

let user: { name: string age: number }

Bu qat'iy structure.

Record type

let scores: Record<string,number>

Bu moslashuvchan object.

🎯 Xulosa

Record type:

Record<KeyType,ValueType>

Misol:

Record<string,number>

Bu degani:

string key number value

💡 Keyingi dars “Working with Enums” bo‘ladi. Bu TypeScript dagi juda muhim va ko‘p ishlatiladigan feature.