Skip to content

Commit 72e8ed6

Browse files
author
Elias Mulhall
committed
Add DecoderResult type aliases
1 parent 3dfebd6 commit 72e8ed6

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/decoder.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ export interface DecoderError {
1313
message: string;
1414
}
1515

16+
/**
17+
* Alias for the result of the `Decoder.run` method. On success returns `Ok`
18+
* with the decoded value of type `A`, on failure returns `Err` containing a
19+
* `DecoderError`.
20+
*/
21+
type RunResult<A> = Result.Result<A, DecoderError>;
22+
23+
/**
24+
* Alias for the result of the internal `Decoder.decode` method. Since `decode`
25+
* is a private function it returns a partial decoder error on failure, which
26+
* will be completed and polished when handed off to the `run` method.
27+
*/
28+
type DecodeResult<A> = Result.Result<A, Partial<DecoderError>>;
29+
1630
/**
1731
* Defines a mapped type over an interface `A`. `DecoderObject<A>` is an
1832
* interface that has all the keys or `A`, but each key's property type is
@@ -120,7 +134,7 @@ export class Decoder<A> {
120134
* `andThen` and `map` should be enough to build specialized decoders as
121135
* needed.
122136
*/
123-
private constructor(private decode: (json: any) => Result.Result<A, Partial<DecoderError>>) {}
137+
private constructor(private decode: (json: any) => DecodeResult<A>) {}
124138

125139
/**
126140
* Decoder primitive that validates strings, and fails on all other input.
@@ -311,12 +325,12 @@ export class Decoder<A> {
311325
*/
312326
static array = <A>(decoder: Decoder<A>): Decoder<A[]> =>
313327
new Decoder<A[]>(json => {
314-
const decodeValue = (v: any, i: number): Result.Result<A, Partial<DecoderError>> =>
328+
const decodeValue = (v: any, i: number): DecodeResult<A> =>
315329
Result.mapError(err => prependAt(`[${i}]`, err), decoder.decode(v));
316330

317331
return isJsonArray(json)
318332
? json.reduce(
319-
(acc: Result.Result<A[], Partial<DecoderError>>, v: any, i: number) =>
333+
(acc: DecodeResult<A[]>, v: any, i: number) =>
320334
Result.map2((arr, result) => [...arr, result], acc, decodeValue(v, i)),
321335
Result.ok([])
322336
)
@@ -602,7 +616,7 @@ export class Decoder<A> {
602616
* // }
603617
* ```
604618
*/
605-
run = (json: any): Result.Result<A, DecoderError> =>
619+
run = (json: any): RunResult<A> =>
606620
Result.mapError(
607621
error => ({
608622
kind: 'DecoderError' as 'DecoderError',

0 commit comments

Comments
 (0)