-
Notifications
You must be signed in to change notification settings - Fork 0
/
00007-easy-readonly.ts
55 lines (41 loc) · 1.24 KB
/
00007-easy-readonly.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
7 - Readonly
-------
by Anthony Fu (@antfu) #easy #built-in #readonly #object-keys
### Question
Implement the built-in `Readonly<T>` generic without using it.
Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.
For example
```ts
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
```
> View on GitHub: https://tsch.js.org/7
*/
/* _____________ Your Code Here _____________ */
type MyReadonly<T> = { readonly [K in keyof T]: T[K] }
/* _____________ 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
}
}
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/7/answer
> View solutions: https://tsch.js.org/7/solutions
> More Challenges: https://tsch.js.org
*/