Complete the code to make the object properties read-only using the Readonly utility type.
type User = [1]<{ name: string; age: number }>;The Readonly utility type makes all properties of the given type read-only.
Complete the code to create a read-only object and try to assign a new value to a property (which should cause an error).
const user: Readonly<{ name: string }> = { name: 'Alice' };
user.[1] = 'Bob';Assigning to name property causes an error because user is read-only.
Fix the error in the code by using the Readonly utility type correctly.
interface Config {
url: string;
timeout: number;
}
const config: [1]<Config> = {
url: 'https://api.example.com',
timeout: 5000
};
config.timeout = 3000;Using Readonly makes config properties immutable, so assignment to timeout causes an error.
Fill both blanks to create a read-only array of strings and try to push a new element (which should cause an error).
const fruits: [1]<string[]> = ['apple', 'banana']; fruits.[2]('orange');
The Readonly utility type makes the array immutable, so calling push causes an error.
Fill all three blanks to define a read-only object type, create an instance, and attempt to modify a property (which should cause an error).
type [1]<T> = { readonly [P in keyof T]: T[P]; }; interface Person { name: string; age: number; } const person: [2]<Person> = { name: 'John', age: 25 }; person.[3] = 30;
The custom Readonly type makes all properties read-only. Trying to assign to age causes an error.