-
Notifications
You must be signed in to change notification settings - Fork 0
/
00008-medium-readonly-2.ts
42 lines (34 loc) · 1.08 KB
/
00008-medium-readonly-2.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
// ============= Test Cases =============
import type { Alike, Expect } from './test-utils'
type cases = [
Expect<Alike<MyReadonly2<Todo1>, Readonly<Todo1>>>,
Expect<Alike<MyReadonly2<Todo1, 'title' | 'description'>, Expected>>,
Expect<Alike<MyReadonly2<Todo2, 'title' | 'description'>, Expected>>,
Expect<Alike<MyReadonly2<Todo2, 'description'>, Expected>>
]
// @ts-expect-error
type error = MyReadonly2<Todo1, 'title' | 'invalid'>
interface Todo1 {
title: string
description?: string
completed: boolean
}
interface Todo2 {
readonly title: string
description?: string
completed: boolean
}
interface Expected {
readonly title: string
readonly description?: string
completed: boolean
}
// ============= Your Code Here =============
// type MyReadonly2<T, K extends keyof T = keyof T> = {
// readonly [P in K]: T[P]
// } & {
// [P in keyof T as P extends K ? never : P]: T[P]
// }
// type MyReadonly2<T, K extends keyof T = keyof T> = Readonly<Pick<T, K>> & Pick<T, Exclude<keyof T, K>>;
type MyReadonly2<T, K extends keyof T = keyof T> = Readonly<Pick<T, K>> &
Omit<T, K>