MyReadonly<T>
EasyP1Coding
Implement the built-in Readonly<T> generic without using it.
MyReadonly<T> builds a type with every property of T marked readonly, so assigning to any property is a compile error.
Tests your type must pass
interface Todo1 {
title: string
description: string
completed: boolean
meta: {
author: string
}
}
declare const todo: MyReadonly<Todo1>
type cases = [
Expect<Equal<MyReadonly<Todo1>, Readonly<Todo1>>>,
Expect<Equal<keyof MyReadonly<Todo1>, 'title' | 'description' | 'completed' | 'meta'>>,
]
// @ts-expect-error title is readonly
todo.title = 'Hello'
// @ts-expect-error description is readonly
todo.description = 'barFoo'
// @ts-expect-error completed is readonly
todo.completed = trueHints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.