Kontrol Tipe Nilai Objek di TypeScript

Pada saat deklarasi objek, TypeScript mengingat tipe data semua elemennya, kemudian mengontrol agar tipe-tipe tersebut tidak berubah.

Mari kita lihat contoh objek user kita. Deklarasikan objeknya:

typescriptlet user = {name: 'john', age: 30};

Setelah deklarasi, TypeScript menganalisis tipe data setiap nilai dan mengingat bahwa di field 'name' - string, dan di field 'age' - number.

Sekarang upaya untuk menulis nilai dengan tipe yang berbeda ke field akan berakhir dengan error.

Contoh:

typescriptuser.name = 123; // error

Contoh:

typescriptuser.age = 'eee'; // error

Contoh:

typescriptuser.age = '30'; // error
Задание 1

Tanpa menjalankan kode, tentukan bagaimana hasil eksekusi kode:

typescriptlet date = {year: 2025, month: 12, day: 31};
date.month = '12';

console.log(date);
Задание 2

Tanpa menjalankan kode, tentukan bagaimana hasil eksekusi kode:

typescriptlet product = {code: '123', name: 'apple', price: 12};
product.code = 123;

console.log(product);
Задание 3

Tanpa menjalankan kode, tentukan bagaimana hasil eksekusi kode:

typescriptlet product = {code: '123', name: 'apple', price: 12 };
product.price = 123;

console.log(product);
Задание 4

Tanpa menjalankan kode, tentukan bagaimana hasil eksekusi kode:

typescriptlet user = {name: 'john', admin: true};
user.admin = 'false';

console.log(user);