decoders 2.2.0 → 2.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  interface ObjectAnnotation {
2
2
  readonly type: 'object';
3
- readonly fields: {
4
- readonly [key: string]: Annotation;
5
- };
3
+ readonly fields: ReadonlyMap<string, Annotation>;
6
4
  readonly text?: string;
7
5
  }
8
6
  interface ArrayAnnotation {
@@ -55,8 +53,12 @@ declare function ok<T>(value: T): Ok<T>;
55
53
  */
56
54
  declare function err<E>(error: E): Err<E>;
57
55
 
58
- type Scalar = string | number | boolean | symbol | undefined | null;
59
56
  type DecodeResult<T> = Result<T, Annotation>;
57
+ /**
58
+ * A function taking a untrusted input, and returning a DecodeResult<T>. The
59
+ * `ok()` and `err()` constructor functions are provided as the 2nd and 3rd
60
+ * param. One of these should be called and its value returned.
61
+ */
60
62
  type AcceptanceFn<T, InputT = unknown> = (blob: InputT, ok: (value: T) => DecodeResult<T>, err: (msg: string | Annotation) => DecodeResult<T>) => DecodeResult<T>;
61
63
  interface Decoder<T> {
62
64
  /**
@@ -94,24 +96,37 @@ interface Decoder<T> {
94
96
  */
95
97
  describe(message: string): Decoder<T>;
96
98
  /**
97
- * Chain together the current decoder with another acceptance function.
99
+ * Send the output of the current decoder into another acceptance function.
100
+ * The given acceptance function will receive the output of the current
101
+ * decoder as its input, making it partially trusted.
102
+ *
103
+ * This works similar to how you would `define()` a new decoder, except
104
+ * that the ``blob`` param will now be ``T`` (a known type), rather than
105
+ * ``unknown``. This will allow the function to make a stronger assumption
106
+ * about its input and avoid re-refining inputs.
107
+ *
108
+ * > _**NOTE:** This is an advanced, low-level, API. It's not recommended
109
+ * > to reach for this construct unless there is no other way. Most cases can
110
+ * > be covered more elegantly by `.transform()` or `.refine()` instead._
111
+ *
112
+ * If it helps, you can think of `define(...)` as equivalent to
113
+ * `unknown.then(...)`.
98
114
  */
99
115
  then<V>(next: AcceptanceFn<V, T>): Decoder<V>;
100
- peek_UNSTABLE<V>(next: AcceptanceFn<V, [unknown, T]>): Decoder<V>;
101
116
  }
102
117
  /**
103
- * Helper type to return the "type" of a Decoder.
118
+ * Helper type to return the output type of a Decoder.
119
+ * It’s the inverse of Decoder<T>.
104
120
  *
105
- * You can use it on types:
121
+ * You can use it at the type level:
106
122
  *
107
123
  * DecoderType<Decoder<string>> // string
108
124
  * DecoderType<Decoder<number[]>> // number[]
109
125
  *
110
- * Or on "values", by using the `typeof` keyword:
126
+ * Or on decoder instances, by using the `typeof` keyword:
111
127
  *
112
128
  * DecoderType<typeof string> // string
113
129
  * DecoderType<typeof truthy> // boolean
114
- *
115
130
  */
116
131
  type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : never;
117
132
  /**
@@ -129,36 +144,39 @@ type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : neve
129
144
  */
130
145
  declare function define<T>(fn: AcceptanceFn<T>): Decoder<T>;
131
146
 
132
- type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
133
- type JSONObject = {
134
- [key: string]: JSONValue | undefined;
135
- };
136
- type JSONArray = JSONValue[];
147
+ type Formatter = (err: Annotation) => string | Error;
148
+ declare function formatInline(ann: Annotation): string;
149
+ declare function formatShort(ann: Annotation): string;
150
+
137
151
  /**
138
- * Accepts objects that contain only valid JSON values.
152
+ * Accepts any array, but doesn't validate its items further.
153
+ *
154
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
139
155
  */
140
- declare const jsonObject: Decoder<JSONObject>;
156
+ declare const poja: Decoder<unknown[]>;
141
157
  /**
142
- * Accepts arrays that contain only valid JSON values.
158
+ * Accepts arrays of whatever the given decoder accepts.
143
159
  */
144
- declare const jsonArray: Decoder<JSONArray>;
160
+ declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
145
161
  /**
146
- * Accepts any value that's a valid JSON value.
147
- *
148
- * In other words: any value returned by `JSON.parse()` should decode without
149
- * failure.
150
- *
151
- * ```typescript
152
- * type JSONValue =
153
- * | null
154
- * | string
155
- * | number
156
- * | boolean
157
- * | { [string]: JSONValue }
158
- * | JSONValue[]
159
- * ```
162
+ * Like `array()`, but will reject arrays with 0 elements.
160
163
  */
161
- declare const json: Decoder<JSONValue>;
164
+ declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
165
+ /**
166
+ * Similar to `array()`, but returns the result as an [ES6
167
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
168
+ */
169
+ declare function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
170
+ type TupleDecoderType<Ds extends readonly Decoder<unknown>[]> = {
171
+ [K in keyof Ds]: DecoderType<Ds[K]>;
172
+ };
173
+ /**
174
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
175
+ * _n_ given decoders.
176
+ */
177
+ declare function tuple<Ds extends readonly [first: Decoder<unknown>, ...rest: readonly Decoder<unknown>[]]>(...decoders: Ds): Decoder<TupleDecoderType<Ds>>;
178
+
179
+ type Scalar = string | number | boolean | symbol | undefined | null;
162
180
 
163
181
  /**
164
182
  * Accepts and returns only the literal `null` value.
@@ -186,15 +204,20 @@ declare function optional<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V
186
204
  declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
187
205
  declare function nullable<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
188
206
  declare function nullable<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
207
+ /**
208
+ * @deprecated
209
+ * Alias of nullish().
210
+ */
211
+ declare const maybe: typeof nullish;
189
212
  /**
190
213
  * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
191
214
  *
192
215
  * If a default value is explicitly provided, return that instead in the
193
216
  * `null`/`undefined` case.
194
217
  */
195
- declare function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
196
- declare function maybe<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
197
- declare function maybe<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
218
+ declare function nullish<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
219
+ declare function nullish<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
220
+ declare function nullish<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
198
221
  /**
199
222
  * Accepts only the given constant value.
200
223
  */
@@ -208,6 +231,16 @@ declare function constant<C extends Scalar>(value: C): Decoder<C>;
208
231
  declare function always<C extends Scalar>(value: C): Decoder<C>;
209
232
  declare function always<T>(value: (() => T) | T): Decoder<T>;
210
233
  /**
234
+ * Rejects all inputs, and always fails with the given error message. May be
235
+ * useful for explicitly disallowing keys, or for testing purposes.
236
+ */
237
+ declare function never(msg: string): Decoder<never>;
238
+ /**
239
+ * Alias of never().
240
+ */
241
+ declare const fail: typeof never;
242
+ /**
243
+ * @deprecated
211
244
  * Alias of always.
212
245
  */
213
246
  declare const hardcoded: typeof always;
@@ -220,40 +253,11 @@ declare const hardcoded: typeof always;
220
253
  */
221
254
  declare const unknown: Decoder<unknown>;
222
255
  /**
256
+ * @deprecated
223
257
  * Alias of unknown.
224
258
  */
225
259
  declare const mixed: Decoder<unknown>;
226
260
 
227
- /**
228
- * Accepts any array, but doesn't validate its items further.
229
- *
230
- * "poja" means "plain old JavaScript array", a play on `pojo()`.
231
- */
232
- declare const poja: Decoder<unknown[]>;
233
- /**
234
- * Accepts arrays of whatever the given decoder accepts.
235
- */
236
- declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
237
- /**
238
- * Like `array()`, but will reject arrays with 0 elements.
239
- */
240
- declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
241
- /**
242
- * Similar to `array()`, but returns the result as an [ES6
243
- * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
244
- */
245
- declare function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
246
- /**
247
- * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
248
- * _n_ given decoders.
249
- */
250
- declare function tuple<A>(a: Decoder<A>): Decoder<[A]>;
251
- declare function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
252
- declare function tuple<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
253
- declare function tuple<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
254
- declare function tuple<A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
255
- declare function tuple<A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
256
-
257
261
  /**
258
262
  * Accepts and returns booleans.
259
263
  */
@@ -280,9 +284,94 @@ declare const date: Decoder<Date>;
280
284
  */
281
285
  declare const iso8601: Decoder<Date>;
282
286
 
287
+ type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
288
+ type JSONObject = {
289
+ [key: string]: JSONValue | undefined;
290
+ };
291
+ type JSONArray = JSONValue[];
292
+ /**
293
+ * Accepts objects that contain only valid JSON values.
294
+ */
295
+ declare const jsonObject: Decoder<JSONObject>;
296
+ /**
297
+ * Accepts arrays that contain only valid JSON values.
298
+ */
299
+ declare const jsonArray: Decoder<JSONArray>;
300
+ /**
301
+ * Accepts any value that's a valid JSON value.
302
+ *
303
+ * In other words: any value returned by `JSON.parse()` should decode without
304
+ * failure.
305
+ *
306
+ * ```typescript
307
+ * type JSONValue =
308
+ * | null
309
+ * | string
310
+ * | number
311
+ * | boolean
312
+ * | { [string]: JSONValue }
313
+ * | JSONValue[]
314
+ * ```
315
+ */
316
+ declare const json: Decoder<JSONValue>;
317
+
318
+ interface Klass<T> extends Function {
319
+ new (...args: readonly any[]): T;
320
+ }
321
+ type Instance<K> = K extends Klass<infer T> ? T : never;
322
+ /**
323
+ * Accepts any value that is an ``instanceof`` the given class.
324
+ */
325
+ declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
326
+ /**
327
+ * Lazily evaluate the given decoder. This is useful to build self-referential
328
+ * types for recursive data structures.
329
+ */
330
+ declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
331
+ /**
332
+ * Pre-process the data input before passing it into the decoder. This gives
333
+ * you the ability to arbitrarily customize the input on the fly before passing
334
+ * it to the decoder. Of course, the input value at that point is still of
335
+ * ``unknown`` type, so you will have to deal with that accordingly.
336
+ */
337
+ declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
338
+
339
+ /**
340
+ * Accepts any valid ``number`` value.
341
+ *
342
+ * This also accepts special values like `NaN` and `Infinity`. Unless you
343
+ * want to deliberately accept those, you'll likely want to use the
344
+ * `number` decoder instead.
345
+ */
346
+ declare const anyNumber: Decoder<number>;
347
+ /**
348
+ * Accepts finite numbers (can be integer or float values). Values `NaN`,
349
+ * or positive and negative `Infinity` will get rejected.
350
+ */
351
+ declare const number: Decoder<number>;
352
+ /**
353
+ * Accepts only finite whole numbers.
354
+ */
355
+ declare const integer: Decoder<number>;
356
+ /**
357
+ * Accepts only non-negative (zero or positive) finite numbers.
358
+ */
359
+ declare const positiveNumber: Decoder<number>;
360
+ /**
361
+ * Accepts only non-negative (zero or positive) finite whole numbers.
362
+ */
363
+ declare const positiveInteger: Decoder<number>;
364
+ /**
365
+ * Accepts any valid ``bigint`` value.
366
+ */
367
+ declare const bigint: Decoder<bigint>;
368
+
283
369
  type RequiredKeys<T extends object> = {
284
370
  [K in keyof T]: undefined extends T[K] ? never : K;
285
371
  }[keyof T];
372
+ type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
373
+ [K in keyof T]: T[K];
374
+ };
286
375
  /**
287
376
  * Transforms an object type, by marking all fields that contain "undefined"
288
377
  * with a question mark, i.e. allowing implicit-undefineds when
@@ -305,12 +394,8 @@ type RequiredKeys<T extends object> = {
305
394
  * }
306
395
  */
307
396
  type UndefinedToOptional<T extends object> = Resolve<Pick<Required<T>, RequiredKeys<T>> & Partial<T>>;
308
- type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
309
- [K in keyof T]: T[K];
310
- };
311
-
312
- type ObjectDecoderType<T> = UndefinedToOptional<{
313
- [K in keyof T]: T[K] extends Decoder<infer V> ? V : never;
397
+ type ObjectDecoderType<Ds extends Record<string, Decoder<unknown>>> = UndefinedToOptional<{
398
+ [K in keyof Ds]: DecoderType<Ds[K]>;
314
399
  }>;
315
400
  /**
316
401
  * Accepts any "plain old JavaScript object", but doesn't validate its keys or
@@ -321,24 +406,20 @@ declare const pojo: Decoder<Record<string, unknown>>;
321
406
  * Accepts objects with fields matching the given decoders. Extra fields that
322
407
  * exist on the input object are ignored and will not be returned.
323
408
  */
324
- declare function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
325
- declare function object<DS extends Record<string, Decoder<any>>>(decodersByKey: DS): Decoder<ObjectDecoderType<DS>>;
409
+ declare function object(decoders: Record<any, never>): Decoder<Record<string, never>>;
410
+ declare function object<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
326
411
  /**
327
412
  * Like `object()`, but will reject inputs that contain extra fields that are
328
413
  * not specified explicitly.
329
414
  */
330
- declare function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
331
- declare function exact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
332
- [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
333
- }>;
415
+ declare function exact(decoders: Record<any, never>): Decoder<Record<string, never>>;
416
+ declare function exact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
334
417
  /**
335
418
  * Like `object()`, but will pass through any extra fields on the input object
336
419
  * unvalidated that will thus be of `unknown` type statically.
337
420
  */
338
- declare function inexact(decodersByKey: Record<any, never>): Decoder<Record<string, unknown>>;
339
- declare function inexact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
340
- [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
341
- } & Record<string, unknown>>;
421
+ declare function inexact(decoders: Record<any, never>): Decoder<Record<string, unknown>>;
422
+ declare function inexact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds> & Record<string, unknown>>;
342
423
  /**
343
424
  * Accepts objects where all values match the given decoder, and returns the
344
425
  * result as a `Record<string, T>`.
@@ -357,53 +438,6 @@ declare function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
357
438
  */
358
439
  declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
359
440
 
360
- type Values<T extends object> = T[keyof T];
361
- type DecoderTypes<T> = T extends readonly Decoder<infer U>[] ? U : never;
362
- /**
363
- * Accepts values accepted by any of the given decoders.
364
- *
365
- * The decoders are tried on the input one by one, in the given order. The
366
- * first one that accepts the input "wins". If all decoders reject the input,
367
- * the input gets rejected.
368
- */
369
- declare function either<T extends readonly Decoder<any>[]>(...decoders: T): Decoder<DecoderTypes<T>>;
370
- /**
371
- * Accepts any value that is strictly-equal (using `===`) to one of the
372
- * specified values.
373
- */
374
- declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
375
- /**
376
- * If you are decoding tagged unions you may want to use the `taggedUnion()`
377
- * decoder instead of the general purpose `either()` decoder to get better
378
- * error messages and better performance.
379
- *
380
- * This decoder is optimized for [tagged
381
- * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
382
- * objects where one field is used as the discriminator.
383
- *
384
- * ```ts
385
- * const A = object({ tag: constant('A'), foo: string });
386
- * const B = object({ tag: constant('B'), bar: number });
387
- *
388
- * const AorB = taggedUnion('tag', { A, B });
389
- * // ^^^
390
- * ```
391
- *
392
- * Decoding now works in two steps:
393
- *
394
- * 1. Look at the `'tag'` field in the incoming object (this is the field
395
- * that decides which decoder will be used)
396
- * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
397
- * decoder `B` will be used. Otherwise, this will fail.
398
- *
399
- * This is effectively equivalent to `either(A, B)`, but will provide better
400
- * error messages and is more performant at runtime because it doesn't have to
401
- * try all decoders one by one.
402
- */
403
- declare function taggedUnion<O extends Record<string, Decoder<any>>>(field: string, mapping: O): Decoder<Values<{
404
- [key in keyof O]: DecoderType<O[key]>;
405
- }>>;
406
-
407
441
  /**
408
442
  * Accepts and returns strings.
409
443
  */
@@ -449,64 +483,57 @@ declare const uuidv1: Decoder<string>;
449
483
  */
450
484
  declare const uuidv4: Decoder<string>;
451
485
 
452
- interface Klass<T> extends Function {
453
- new (...args: readonly any[]): T;
454
- }
455
- type Instance<K> = K extends Klass<infer T> ? T : never;
456
- /**
457
- * Accepts any value that is an ``instanceof`` the given class.
458
- */
459
- declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
460
- /**
461
- * Lazily evaluate the given decoder. This is useful to build self-referential
462
- * types for recursive data structures.
463
- */
464
- declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
465
- /**
466
- * Pre-process the data input before passing it into the decoder. This gives
467
- * you the ability to arbitrarily customize the input on the fly before passing
468
- * it to the decoder. Of course, the input value at that point is still of
469
- * ``unknown`` type, so you will have to deal with that accordingly.
470
- */
471
- declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
472
- /**
473
- * Rejects all inputs, and always fails with the given error message. May be
474
- * useful for explicitly disallowing keys, or for testing purposes.
475
- */
476
- declare function never(msg: string): Decoder<never>;
477
- /**
478
- * Alias of never().
479
- */
480
- declare const fail: typeof never;
481
-
482
486
  /**
483
- * Accepts any valid ``number`` value.
487
+ * Accepts values accepted by any of the given decoders.
484
488
  *
485
- * This also accepts special values like `NaN` and `Infinity`. Unless you
486
- * want to deliberately accept those, you'll likely want to use the
487
- * `number` decoder instead.
488
- */
489
- declare const anyNumber: Decoder<number>;
490
- /**
491
- * Accepts finite numbers (can be integer or float values). Values `NaN`,
492
- * or positive and negative `Infinity` will get rejected.
489
+ * The decoders are tried on the input one by one, in the given order. The
490
+ * first one that accepts the input "wins". If all decoders reject the input,
491
+ * the input gets rejected.
493
492
  */
494
- declare const number: Decoder<number>;
493
+ declare function either<TDecoders extends readonly Decoder<unknown>[], T = DecoderType<TDecoders[number]>>(...decoders: TDecoders): Decoder<T>;
495
494
  /**
496
- * Accepts only finite whole numbers.
495
+ * Accepts any value that is strictly-equal (using `===`) to one of the
496
+ * specified values.
497
497
  */
498
- declare const integer: Decoder<number>;
498
+ declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
499
499
  /**
500
- * Accepts only non-negative (zero or positive) finite numbers.
500
+ * If you are decoding tagged unions you may want to use the `taggedUnion()`
501
+ * decoder instead of the general purpose `either()` decoder to get better
502
+ * error messages and better performance.
503
+ *
504
+ * This decoder is optimized for [tagged
505
+ * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
506
+ * objects where one field is used as the discriminator.
507
+ *
508
+ * ```ts
509
+ * const A = object({ tag: constant('A'), foo: string });
510
+ * const B = object({ tag: constant('B'), bar: number });
511
+ *
512
+ * const AorB = taggedUnion('tag', { A, B });
513
+ * // ^^^
514
+ * ```
515
+ *
516
+ * Decoding now works in two steps:
517
+ *
518
+ * 1. Look at the `'tag'` field in the incoming object (this is the field
519
+ * that decides which decoder will be used)
520
+ * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
521
+ * decoder `B` will be used. Otherwise, this will fail.
522
+ *
523
+ * This is effectively equivalent to `either(A, B)`, but will provide better
524
+ * error messages and is more performant at runtime because it doesn't have to
525
+ * try all decoders one by one.
501
526
  */
502
- declare const positiveNumber: Decoder<number>;
527
+ declare function taggedUnion<O extends Record<string, Decoder<unknown>>, T = DecoderType<O[keyof O]>>(field: string, mapping: O): Decoder<T>;
503
528
  /**
504
- * Accepts only non-negative (zero or positive) finite whole numbers.
529
+ * Briefly peek at a runtime input using a "scout" decoder first, then decide
530
+ * which decoder to run on the (original) input, based on the information that
531
+ * the "scout" extracted.
532
+ *
533
+ * It serves a similar purpose as `taggedUnion()`, but is a generalization that
534
+ * works even if there isn't a single discriminator, or the discriminator isn't
535
+ * a string.
505
536
  */
506
- declare const positiveInteger: Decoder<number>;
507
-
508
- type Formatter = (err: Annotation) => string | Error;
509
- declare function formatInline(ann: Annotation): string;
510
- declare function formatShort(ann: Annotation): string;
537
+ declare function select<T, D extends Decoder<unknown>>(scout: Decoder<T>, selectFn: (result: T) => D): Decoder<DecoderType<D>>;
511
538
 
512
- export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, always, anyNumber, array, boolean, constant, date, define, dict, either, email, err, exact, fail, formatInline, formatShort, hardcoded, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, number, numericBoolean, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, regex, set, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };
539
+ export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, always, anyNumber, array, bigint, boolean, constant, date, define, dict, either, email, err, exact, fail, formatInline, formatShort, hardcoded, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numericBoolean, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, regex, select, set, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };