TypeScript massivlar (arrays)
TypeScript-da massivlarni yozish uchun maxsus sintaksis mavjud.
Massivlar haqida ko‘proq ma’lumotni JavaScript Array chapter sahifamizda o‘qing.
Misol
const names: string[] = [];
names.push("Dylan"); // no error
// names.push(3); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
O‘zingiz sinab ko‘ring »
Faqat o‘qish
readonly kalit so‘zi massivlarni o‘zgartirishni oldini oladi.
Misol
const names: readonly string[] = ["Dylan"];
names.push("Jack"); // Error: Property 'push' does not exist on type 'readonly string[]'.
// try removing the readonly modifier and see if it works?
O‘zingiz sinab ko‘ring »
Xulosa yozing
TypeScript, agar uning qiymatlari bo‘lsa, massiv turini aniqlashi mumkin.
Misol
const numbers = [1, 2, 3]; // inferred to type number[]
numbers.push(4); // no error
// comment line below out to see the successful assignment
numbers.push("2"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
let head: number = numbers[0]; // no error
O‘zingiz sinab ko‘ring »
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
