-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02757-medium-partialbykeys.ts
More file actions
38 lines (32 loc) · 921 Bytes
/
Copy path02757-medium-partialbykeys.ts
File metadata and controls
38 lines (32 loc) · 921 Bytes
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
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
import { ExpectFalse, NotEqual } from './test-utils'
interface User {
name: string
age: number
address: string
}
interface UserPartialName {
name?: string
age: number
address: string
}
interface UserPartialNameAndAge {
name?: string
age?: number
address: string
}
type cases = [
Expect<Equal<PartialByKeys<User, 'name'>, UserPartialName>>,
Expect<Equal<PartialByKeys<User, 'name' | 'unknown'>, UserPartialName>>,
Expect<Equal<PartialByKeys<User, 'name' | 'age'>, UserPartialNameAndAge>>,
Expect<Equal<PartialByKeys<User>, Partial<User>>>,
]
// ============= Your Code Here =============
type Simplify<T> = { [K in keyof T]: T[K] };
//
type PartialByKeys<T, K = keyof T> = Simplify<{
[k in keyof T as k extends K ? k : never]? : T[k]
} & {
[k in keyof T as k extends K ? never : k] : T[k]
}>