@@ -30,12 +30,30 @@ interface BaseResult<T, E> extends Iterable<T> {
3030 * there won't be an exception thrown on access.
3131 *
3232 * @param msg the message to throw if no Ok value.
33+ *
34+ * @example
35+ * ```typescript
36+ * let goodResult = Ok(1);
37+ * let badResult = Err(new Error('something went wrong'));
38+ *
39+ * goodResult.expect('goodResult should be a number'); // 1
40+ * badResult.expect('badResult should be a number'); // throws Error("badResult should be a number - Error: something went wrong")
41+ * ```
3342 */
3443 expect ( msg : string ) : T ;
3544
3645 /**
3746 * Returns the contained `Err` value, if exists. Throws an error if not.
3847 * @param msg the message to throw if no Err value.
48+ *
49+ * @example
50+ * ```typescript
51+ * let goodResult = Ok(1);
52+ * let badResult = Err(new Error('something went wrong'));
53+ *
54+ * goodResult.expectErr('goodResult should not be a number'); // throws Error("goodResult should not be a number")
55+ * badResult.expectErr('badResult should not be a number'); // new Error('something went wrong')
56+ * ```
3957 */
4058 expectErr ( msg : string ) : E ;
4159
@@ -52,6 +70,15 @@ interface BaseResult<T, E> extends Iterable<T> {
5270 * Throws if the value is an `Err`, with a message provided by the `Err`'s value and
5371 * [`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
5472 * set to the value.
73+ *
74+ * @example
75+ * ```typescript
76+ * let goodResult = new Ok(1);
77+ * let badResult = new Err(new Error('something went wrong'));
78+ *
79+ * goodResult.unwrap(); // 1
80+ * badResult.unwrap(); // throws Error("something went wrong")
81+ * ```
5582 */
5683 unwrap ( ) : T ;
5784
@@ -64,13 +91,31 @@ interface BaseResult<T, E> extends Iterable<T> {
6491 * Throws if the value is an `Ok`, with a message provided by the `Ok`'s value and
6592 * [`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause)
6693 * set to the value.
94+ *
95+ * @example
96+ * ```typescript
97+ * let goodResult = new Ok(1);
98+ * let badResult = new Err('something went wrong');
99+ *
100+ * goodResult.unwrapErr(); // throws an exception
101+ * badResult.unwrapErr(); // returns 'something went wrong'
102+ * ```
67103 */
68104 unwrapErr ( ) : E ;
69105
70106 /**
71107 * Returns the contained `Ok` value or a provided default.
72108 *
73109 * (This is the `unwrap_or` in rust)
110+ *
111+ * @example
112+ * ```typescript
113+ * let goodResult = Ok(1);
114+ * let badResult = Err(new Error('something went wrong'));
115+ *
116+ * goodResult.unwrapOr(5); // 1
117+ * badResult.unwrapOr(5); // 5
118+ * ```
74119 */
75120 unwrapOr < T2 > ( val : T2 ) : T | T2 ;
76121
@@ -93,6 +138,29 @@ interface BaseResult<T, E> extends Iterable<T> {
93138 /**
94139 * Calls `mapper` if the result is `Ok`, otherwise returns the `Err` value of self.
95140 * This function can be used for control flow based on `Result` values.
141+ *
142+ * @example
143+ * ```typescript
144+ * let goodResult = Ok(1);
145+ * let badResult = Err(new Error('something went wrong'));
146+ *
147+ * goodResult.andThen((num) => new Ok(num + 1)).unwrap(); // 2
148+ * badResult.andThen((num) => new Err(new Error('2nd error'))).unwrap(); // throws Error('something went wrong')
149+ * goodResult.andThen((num) => new Err(new Error('2nd error'))).unwrap(); // throws Error('2nd error')
150+ *
151+ * goodResult
152+ * .andThen((num) => new Ok(num + 1))
153+ * .mapErr((err) => new Error('mapped'))
154+ * .unwrap(); // 2
155+ * badResult
156+ * .andThen((num) => new Err(new Error('2nd error')))
157+ * .mapErr((err) => new Error('mapped'))
158+ * .unwrap(); // throws Error('mapped')
159+ * goodResult
160+ * .andThen((num) => new Err(new Error('2nd error')))
161+ * .mapErr((err) => new Error('mapped'))
162+ * .unwrap(); // throws Error('mapped')
163+ * ```
96164 */
97165 andThen < T2 , E2 > ( mapper : ( val : T ) => Result < T2 , E2 > ) : Result < T2 , E | E2 > ;
98166
@@ -101,6 +169,15 @@ interface BaseResult<T, E> extends Iterable<T> {
101169 * leaving an `Err` value untouched.
102170 *
103171 * This function can be used to compose the results of two functions.
172+ *
173+ * @example
174+ * ```typescript
175+ * let goodResult = Ok(1);
176+ * let badResult = Err(new Error('something went wrong'));
177+ *
178+ * goodResult.map((num) => num + 1).unwrap(); // 2
179+ * badResult.map((num) => num + 1).unwrap(); // throws Error("something went wrong")
180+ * ```
104181 */
105182 map < U > ( mapper : ( val : T ) => U ) : Result < U , E > ;
106183
@@ -109,6 +186,21 @@ interface BaseResult<T, E> extends Iterable<T> {
109186 * leaving an `Ok` value untouched.
110187 *
111188 * This function can be used to pass through a successful result while handling an error.
189+ *
190+ * @example
191+ * ```typescript
192+ * let goodResult = Ok(1);
193+ * let badResult = Err(new Error('something went wrong'));
194+ *
195+ * goodResult
196+ * .map((num) => num + 1)
197+ * .mapErr((err) => new Error('mapped'))
198+ * .unwrap(); // 2
199+ * badResult
200+ * .map((num) => num + 1)
201+ * .mapErr((err) => new Error('mapped'))
202+ * .unwrap(); // throws Error("mapped")
203+ * ```
112204 */
113205 mapErr < F > ( mapper : ( val : E ) => F ) : Result < T , F > ;
114206
@@ -118,13 +210,31 @@ interface BaseResult<T, E> extends Iterable<T> {
118210 *
119211 * If `default` is a result of a function call consider using `mapOrElse` instead, it will
120212 * only evaluate the function when needed.
213+ *
214+ * @example
215+ * ```typescript
216+ * let goodResult = Ok(1);
217+ * let badResult = Err(new Error('something went wrong'));
218+ *
219+ * goodResult.mapOr(0, (value) => -value) // -1
220+ * badResult.mapOr(0, (value) => -value) // 0
221+ * ```
121222 */
122223 mapOr < U > ( default_ : U , mapper : ( val : T ) => U ) : U ;
123224
124225 /**
125226 * Maps a `Result<T, E>` to `Result<U, E>` by either converting `T` to `U` using `mapper`
126227 * (in case of `Ok`) or producing a default value using the `default` function (in case of
127228 * `Err`).
229+ *
230+ * @example
231+ * ```typescript
232+ * let goodResult = Ok(1);
233+ * let badResult = Err(new Error('something went wrong'));
234+ *
235+ * goodResult.mapOrElse((_error) => 0, (value) => -value) // -1
236+ * badResult.mapOrElse((_error) => 0, (value) => -value) // 0
237+ * ```
128238 */
129239 mapOrElse < U > ( default_ : ( error : E ) => U , mapper : ( val : T ) => U ) : U ;
130240
@@ -173,7 +283,14 @@ interface BaseResult<T, E> extends Iterable<T> {
173283 * Contains the error value
174284 */
175285export class ErrImpl < E > implements BaseResult < never , E > {
176- /** An empty Err */
286+ /**
287+ * An empty Err
288+ *
289+ * @example
290+ * ```typescript
291+ * const x: Result<string, void> = Err.EMPTY
292+ * ```
293+ */
177294 static readonly EMPTY = new ErrImpl < void > ( undefined ) ;
178295
179296 isOk ( ) : this is OkImpl < never > {
@@ -296,6 +413,14 @@ export type Err<E> = ErrImpl<E>;
296413 * Contains the success value
297414 */
298415export class OkImpl < T > implements BaseResult < T , never > {
416+ /**
417+ * An empty Ok
418+ *
419+ * @example
420+ * ```typescript
421+ * const x: Result<void, string> = Ok.EMPTY
422+ * ```
423+ */
299424 static readonly EMPTY = new OkImpl < void > ( undefined ) ;
300425
301426 isOk ( ) : this is OkImpl < T > {
@@ -408,6 +533,15 @@ export namespace Result {
408533 /**
409534 * Parse a set of `Result`s, returning an array of all `Ok` values.
410535 * Short circuits with the first `Err` found, if any
536+ *
537+ * @example
538+ * ```typescript
539+ * let results: Result<Topping, GetToppingsError>[] = pizzaToppingNames.map(name => getPizzaToppingByName(name));
540+ *
541+ * let result = Result.all(results); // Result<Topping[], GetToppingsError>
542+ *
543+ * let toppings = result.unwrap(); // toppings is an array of Topping. Could throw GetToppingsError.
544+ * ```
411545 */
412546 export function all < const T extends Result < any , any > [ ] > (
413547 results : T ,
@@ -427,6 +561,15 @@ export namespace Result {
427561 /**
428562 * Parse a set of `Result`s, short-circuits when an input value is `Ok`.
429563 * If no `Ok` is found, returns an `Err` containing the collected error values
564+ *
565+ * @example
566+ * ```typescript
567+ * let connections: Array<Result<string, Error>> = [attempt1(), attempt2(), attempt3()];
568+ *
569+ * let results = Result.any(connections); // Result<string, Error[]>
570+ *
571+ * let url = results.unwrap(); // At least one attempt gave us a successful url
572+ * ```
430573 */
431574 export function any < const T extends Result < any , any > [ ] > (
432575 results : T ,
@@ -449,6 +592,13 @@ export namespace Result {
449592 /**
450593 * Wrap an operation that may throw an Error (`try-catch` style) into checked exception style
451594 * @param op The operation function
595+ *
596+ * @example
597+ * ```typescript
598+ * Result.wrap(() => JSON.parse('{"valid": "json"}')) // Ok({ valid: 'json' }), type: Result<any, unknown>
599+ *
600+ * Result.wrap(() => JSON.parse('not json')) // Err(SyntaxError: ...), type: Result<any, unknown>
601+ * ```
452602 */
453603 export function wrap < T , E = unknown > ( op : ( ) => T ) : Result < T , E > {
454604 try {
@@ -461,6 +611,11 @@ export namespace Result {
461611 /**
462612 * Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style
463613 * @param op The operation function
614+ *
615+ * @example
616+ * ```typescript
617+ * await Result.wrapAsync(() => fetch('/api/data').then(r => r.json())) // Ok(data) or Err(error), type: Result<any, unknown>
618+ * ```
464619 */
465620 export function wrapAsync < T , E = unknown > ( op : ( ) => Promise < T > ) : Promise < Result < T , E > > {
466621 try {
@@ -474,6 +629,13 @@ export namespace Result {
474629
475630 /**
476631 * Partitions a set of results, separating the `Ok` and `Err` values.
632+ *
633+ * @example
634+ * ```typescript
635+ * let results: Result<number, string>[] = [Ok(1), Err('error1'), Ok(2), Err('error2')];
636+ *
637+ * let [numbers, errors] = Result.partition(results); // [ [1, 2], ['error1', 'error2'] ]
638+ * ```
477639 */
478640 export function partition < T extends Result < any , any > [ ] > ( results : T ) : [ ResultOkTypes < T > , ResultErrTypes < T > ] {
479641 return results . reduce (
0 commit comments