-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_guards.ts
executable file
·222 lines (189 loc) · 4.56 KB
/
type_guards.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Result type for handling success/error cases
*
* @typeParam T - Type of success value
* @typeParam E - Type of error value
*/
export type Result<T, E> = OkResult<T, E> | ErrResult<T, E>;
/**
* Represents a successful Result containing a value
*
* @typeParam T - Type of success value
* @typeParam E - Type of error value (unused in Ok case)
*/
export class OkResult<T, E> {
readonly tag: 'ok' = 'ok';
constructor(readonly data: T) {}
isOk(): this is OkResult<T, E> {
return true;
}
isErr(): this is ErrResult<T, E> {
return false;
}
// Utility methods
map<U>(fn: (value: T) => U): Result<U, E> {
return Ok(fn(this.data));
}
unwrap(): T {
return this.data;
}
unwrapOr(_default: T): T {
return this.data;
}
}
/**
* Represents a failed Result containing an error
*
* @typeParam T - Type of success value (unused in Err case)
* @typeParam E - Type of error value
*/
export class ErrResult<T, E> {
readonly tag: 'err' = 'err';
constructor(readonly data: E) {}
isOk(): this is OkResult<T, E> {
return false;
}
isErr(): this is ErrResult<T, E> {
return true;
}
// Utility methods
map<U>(_fn: (value: T) => U): Result<U, E> {
return Err(this.data);
}
unwrap(): never {
throw new Error(`Attempted to unwrap an Err value: ${JSON.stringify(this.data)}`);
}
unwrapOr(defaultValue: T): T {
return defaultValue;
}
}
// Helper functions for Result
/**
* Creates a successful Result
*
* @param value - Value to wrap in Ok
* @returns Result containing success value
*/
export function Ok<T, E>(value: T): Result<T, E> {
return new OkResult(value);
}
/**
* Creates a failed Result
*
* @param error - Error to wrap in Err
* @returns Result containing error value
*/
export function Err<T, E>(error: E): Result<T, E> {
return new ErrResult(error);
}
// Type guards for Result
/**
* Type guard for checking if Result is Ok
*
* @param result - Result to check
* @returns True if Result is Ok, false otherwise
*/
export function isOk<T, E>(result: Result<T, E>): result is OkResult<T, E> {
return result.isOk();
}
/**
* Type guard for checking if Result is Err
*
* @param result - Result to check
* @returns True if Result is Err, false otherwise
*/
export function isErr<T, E>(result: Result<T, E>): result is ErrResult<T, E> {
return result.isErr();
}
/**
* Option type for handling optional values
*
* @typeParam T - Type of contained value
*/
export type Option<T> = SomeOption<T> | NoneOption<T>;
/**
* Represents an Option containing a value
*
* @typeParam T - Type of contained value
*/
export class SomeOption<T> {
readonly tag: 'some' = 'some';
constructor(readonly value: T) {}
isSome(): this is SomeOption<T> {
return true;
}
isNone(): this is NoneOption<T> {
return false;
}
// Utility methods
map<U>(fn: (value: T) => U): Option<U> {
return Some(fn(this.value));
}
unwrap(): T {
return this.value;
}
unwrapOr(_default: T): T {
return this.value;
}
}
/**
* Represents an empty Option
*
* @typeParam T - Type of value (unused in None case)
*/
export class NoneOption<T> {
readonly tag: 'none' = 'none';
isSome(): this is SomeOption<T> {
return false;
}
isNone(): this is NoneOption<T> {
return true;
}
// Utility methods
map<U>(_fn: (value: T) => U): Option<U> {
return None();
}
unwrap(): never {
throw new Error('Attempted to unwrap a None value');
}
unwrapOr(defaultValue: T): T {
return defaultValue;
}
}
// Helper functions for Option
/**
* Creates an Option containing a value
*
* @param value - Value to wrap in Some
* @returns Option containing value
*/
export function Some<T>(value: T): Option<T> {
return new SomeOption(value);
}
/**
* Creates an empty Option
*
* @returns Empty Option
*/
export function None<T>(): Option<T> {
return new NoneOption();
}
// Type guards for Option
/**
* Type guard for checking if Option contains value
*
* @param option - Option to check
* @returns True if Option is Some, false otherwise
*/
export function isSome<T>(option: Option<T>): option is SomeOption<T> {
return option.isSome();
}
/**
* Type guard for checking if Option is empty
*
* @param option - Option to check
* @returns True if Option is None, false otherwise
*/
export function isNone<T>(option: Option<T>): option is NoneOption<T> {
return option.isNone();
}