Complete the code to declare a readonly property in a TypeScript interface.
interface User {
readonly [1]: string;
}The readonly keyword makes the property immutable after initialization. Here, name is the readonly property.
Complete the code to create an object that respects the readonly property.
const user: { readonly id: number } = { id: [1] };The id property is a number and readonly. Assigning 42 as a number is correct.
Fix the error by completing the code to prevent modifying a readonly property.
interface Point {
readonly x: number;
readonly y: number;
}
const p: Point = { x: 10, y: 20 };
p.[1] = 30;Properties x and y are readonly. Trying to assign p.x = 30 causes an error. The blank is the property name being assigned.
Fill both blanks to create a readonly property and assign it correctly.
interface Config {
[1] version: string;
}
const config: Config = { version: [2] };const instead of readonly in interfaceThe property version is declared readonly. The value assigned is a string literal with double quotes.
Fill all three blanks to create a readonly property, assign it, and try to modify it (which should cause an error).
interface Settings {
[1] theme: string;
}
const settings: Settings = { theme: [2] };
settings.[3] = "dark";The property theme is readonly. It is assigned the string "light". Trying to assign settings.theme = "dark" causes an error because theme is readonly.