-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2946-objectentries.ts
51 lines (45 loc) · 1.28 KB
/
2946-objectentries.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
/**
* 2946 - ObjectEntries
*
* Implement the type version of ```Object.entries```
*
* For example
*
* ```typescript
* interface Model {
* name: string;
* age: number;
* locations: string[] | null;
* }
* type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
* ```
*/
/* _____________ Your Code Here _____________ */
type ObjectEntries<T, U = Required<T>> =
{
[K in keyof U]: [
K,
[U[K]] extends [never]
? K extends keyof T
? [T[K]] extends [never]
? never
: undefined
: never
: U[K]
]
}[keyof U];
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface Model {
name: string
age: number
locations: string[] | null
}
type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]
type cases = [
Expect<Equal<ObjectEntries<Model>, ModelEntries>>,
Expect<Equal<ObjectEntries<Partial<Model>>, ModelEntries>>,
Expect<Equal<ObjectEntries<{ key?: undefined }>, ['key', undefined]>>,
Expect<Equal<ObjectEntries<{ key: undefined }>, ['key', undefined]>>,
Expect<Equal<ObjectEntries<{ key: string | undefined }>, ['key', string | undefined]>>,
]