TypeScript·

7 - Readonly

  • #Type Challenges
  • #TypeScript

질문

T의 모든 프로퍼티를 읽기 전용(재할당 불가)으로 바꾸는 내장 제네릭 Readonly<T>를 이를 사용하지 않고 구현하세요.

예시:

1
interface Todo {
2
title: string
3
description: string
4
}
5
6
const todo: MyReadonly<Todo> = {
7
title: "Hey",
8
description: "foobar"
9
}
10
11
todo.title = "Hello" // Error: cannot reassign a readonly property
12
todo.description = "barFoo" // Error: cannot reassign a readonly property
13
14

풀이