-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Open
Description
7 - Readonly
The ability to mark a field as readonly is one of the fundamental tools in the TypeScript toolbelt because it prevents users from mistakenly using a field that you don't want it to be possible to use.
🎥 Video Explanation
🔢 Code
import type { Equal, Expect } from './test-utils'
interface Todo1 {
title: string
description: string
completed: boolean
meta: {
author: string
}// also any[]
}
interface ReadonlyTodo1 {
readonly title: string
readonly description: string
readonly completed: boolean
readonly meta: {
author: string // note, this is not "deep"
}
}
type A1 = MyReadonly<Todo1>;
type B1 = ReadonlyTodo1;
type C1 = Expect<Equal<A1, B1>>;
type A2 = MyReadonly<Todo1>;
type B2 = Readonly<Todo1>;
type C2 = Expect<Equal<A2, B2>>;
// ============= Your Code Here =============
type MyReadonly<T> = {
readonly [key in keyof T]: T[key];
}➕ More Solutions
For more video solutions to other challenges: see the umbrella list! #21338
Metadata
Metadata
Assignees
Labels
No labels