Sobes.tech
Middle+

/* Write the MyReadonly type - makes all fields read-only */ type User = { id: number name: string } type MyReadonly<T> = any const user: MyReadonly<User> = { id: 1, name: "John" } user.name = "Jane"

sobes.tech AI

Answer from AI

The MyReadonly<T> type can be implemented using mapped types in TypeScript by adding the readonly modifier to all fields of type T:

// Makes all properties of type T readonly
type MyReadonly<T> = {
  readonly [P in keyof T]: T[P]
}

// Example usage

type User = {
  id: number
  name: string
}

const user: MyReadonly<User> = { id: 1, name: "John" }

// Error: cannot modify readonly properties
user.name = "Jane" // Compilation error

Thus, MyReadonly creates a new type where all fields of the original type are marked as readonly, preventing their modification after initialization.

/* Write the MyReadonly type - makes all fields… - sobes.tech