Go Back

Refer: Easy - Readonly

Relation: Medium - Readonly-2

Describe

Implement the built-in Readonly<T> generic without using it.

Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.

For example

code snippetCopyjavascript
interface Todo { title: string description: string } const todo: MyReadonly<Todo> = { title: "Hey", description: "foobar" } todo.title = "Hello" // Error: cannot reassign a readonly property todo.description = "barFoo" // Error: cannot reassign a readonly property

Test Cases

code snippetCopytypescript
import { Equal, Expect } from '@type-challenges/utils' type cases = [Expect<Equal<MyReadonly<Todo1>, Readonly<Todo1>>>] interface Todo1 { title: string description: string completed: boolean meta: { author: string } }

Solution

code snippetCopytypescript
type MyReadonly<T> = { readonly [P in keyof T]: T[P] }