Type checking of object values in TypeScript
When an object is declared, TypeScript remembers the data type of all its elements, and then ensures that those types don't change.
Let's look at the example of our object with the user. Let's declare it:
typescriptlet user = {name: 'john', age: 30};
After the declaration, TypeScript analyzed the data type of each value and remembered that 'name' is a string, and 'age' is a number.
Now trying to write a value of a different type to the field will result in an error.
Example:
typescriptuser.name = 123; //
Example:
typescriptuser.age = 'eee'; //
Example:
typescriptuser.age = '30'; //
Without running the code, determine what the result of executing the code will be:
typescriptlet date = {year: 2025, month: 12, day: 31};
date.month = '12';
console.log(date);
Without running the code, determine what the result of executing the code will be:
typescriptlet product = {code: '123', name: 'apple', price: 12};
product.code = 123;
console.log(product);
Without running the code, determine what the result of executing the code will be:
typescriptlet product = {code: '123', name: 'apple', price: 12 };
product.price = 123;
console.log(product);
Without running the code, determine what the result of executing the code will be:
typescriptlet user = {name: 'john', admin: true};
user.admin = 'false';
console.log(user);