-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexMappedType.ts
98 lines (91 loc) · 1.64 KB
/
indexMappedType.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
type SomeObject = {
a: string;
b: number;
}
/**
* | {
* key: 'a';
* }
* | {
* key: 'b';
* }
*/
type UnionKey = {
[K in keyof SomeObject]: {
key: K;
}
}[keyof SomeObject];
type Fruit = "apple" | "banana" | "orange";
/**
* | {
* thisFruit: 'apple';
* allFruit: 'apple' | 'banana' | 'orange';
* }
* | {
* thisFruit: 'banana';
* allFruit: 'apple' | 'banana' | 'orange';
* }
* | {
* thisFruit: 'orange';
* allFruit: 'apple' | 'banana' | 'orange';
* }
*/
type FruitInfo = {
[K in Fruit]: {
thisFruit: K
allFruit: Fruit
restOfTheFruit: Exclude<Fruit, K>
}
}[Fruit]
type PaymentProvider = "adyen" | "paypal"
// Create a new type, having:
// - paymentProvider of type Payment Provider
// - payload object, having encryptedNonce as a key
// Now this type can be pattern-matched via paymentProvider key
type PaymentPayload = {
[Provider in PaymentProvider]: {
paymentProvider: Provider
payload: {
encryptedNonce: string
}
}
}[PaymentProvider]
// HTTP response code
type SuccessResponseCode = 200 | 201
type FailureResponseCode = 400 | 500
type HttpResponseCode = SuccessResponseCode | FailureResponseCode
/**
* | {
* code: 200;
* body: {
* success: true;
* };
* }
* | {
* code: 400;
* body: {
* success: false;
* error: string;
* };
* }
* | {
* code: 500;
* body: {
* success: false;
* error: string;
* };
* }
*/
type HttpResponse = {
[Code in HttpResponseCode]: {
code: Code
body: Code extends SuccessResponseCode ?
{
success: true
} :
{
success: false
error: Code
}
}
}[HttpResponseCode]