문제
T의 모든 프로퍼티를 읽기 전용(재할당 불가)으로 바꾸는 내장 제네릭 Readonly를 사용하지 않고 구현하세요.
예시
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
풀이
type MyReadonly<T> = {
readonly [key in keyof T]: T[key]
}
Readonly
type MyReadonly<T> = Readonly<T>;
'TypeScript > Type Challenges' 카테고리의 다른 글
[43] Exclude (0) | 2022.11.26 |
---|---|
[18] Length of Tuple (0) | 2022.11.19 |
[14] easy first (0) | 2022.11.16 |
[11] Tuple to Object (0) | 2022.11.09 |
[4] Pick (0) | 2022.10.29 |