-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4179-flip.ts
42 lines (32 loc) · 1.26 KB
/
4179-flip.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
/*
4179 - Flip
-------
by Farhan Kathawala (@kathawala) #medium #object
### Question
Implement the type of `just-flip-object`. Examples:
```typescript
Flip<{ a: "x", b: "y", c: "z" }>; // {x: 'a', y: 'b', z: 'c'}
Flip<{ a: 1, b: 2, c: 3 }>; // {1: 'a', 2: 'b', 3: 'c'}
Flip<{ a: false, b: true }>; // {false: 'a', true: 'b'}
```
No need to support nested objects and values which cannot be object keys such as arrays
> View on GitHub: https://tsch.js.org/4179
*/
/* _____________ Your Code Here _____________ */
type Flip<T extends Record<PropertyKey, any>> = {
[key in keyof T as T[key] extends PropertyKey ? T[key] : `${T[key]}`]: key;
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect, NotEqual } from '@type-challenges/utils'
type cases = [
Expect<Equal<{ a: 'pi' }, Flip<{ pi: 'a' }>>>,
Expect<NotEqual<{ b: 'pi' }, Flip<{ pi: 'a' }>>>,
Expect<Equal<{ 3.14: 'pi'; true: 'bool' }, Flip<{ pi: 3.14; bool: true }>>>,
Expect<Equal<{ val2: 'prop2'; val: 'prop' }, Flip<{ prop: 'val'; prop2: 'val2' }>>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/4179/answer
> View solutions: https://tsch.js.org/4179/solutions
> More Challenges: https://tsch.js.org
*/