这是indexloc提供的服务,不要输入任何密码
Skip to content

7 - Readonly (🎥 Video Explanation and Solution) #21439

@dimitropoulos

Description

@dimitropoulos

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

Release Date: 2023-01-03 19:00 UTC

Readonly

🔢 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions