✍ 공부/TypeScript
[type-challenges] Readonly
Po_tta_tt0
2023. 5. 16. 14:41
반응형
0. 문제
Readonly<T>
쓰지 않고 readonly 만들기
1. 설명
/* _____________ Your Code Here _____________ */
type MyReadonly<T> = {readonly [key in keyof T]: T[key]}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<MyReadonly<Todo1>, Readonly<Todo1>>>,
]
interface Todo1 {
title: string
description: string
completed: boolean
meta: {
author: string
}
}
- MyReadonly는 T를 받아서 [key in keyof T]를 key로 두고
- T[key]를 type으로 둔다.
- 여기까지 만들어진 결과는
이다.type myType = { title: string; description: string; completed: boolean; meta: { author: string; }; }
- 여기에 readonly를 추가함으로써 readonly로 변경할 수 있다.
type myType = { readonly title: string; readonly description: string; readonly completed: boolean; readonly meta: { author: string; }; }
반응형