|
1 | 1 | import { Infer, Struct } from '../struct'
|
2 | 2 | import { define } from './utilities'
|
3 | 3 | import {
|
4 |
| - TupleSchema, |
5 | 4 | ObjectSchema,
|
6 | 5 | ObjectType,
|
7 | 6 | print,
|
8 | 7 | run,
|
9 | 8 | isObject,
|
| 9 | + AnyStruct, |
| 10 | + InferStructTuple, |
| 11 | + UnionToIntersection, |
10 | 12 | } from '../utils'
|
11 | 13 |
|
12 | 14 | /**
|
@@ -164,59 +166,9 @@ export function integer(): Struct<number, null> {
|
164 | 166 | * Ensure that a value matches all of a set of types.
|
165 | 167 | */
|
166 | 168 |
|
167 |
| -export function intersection<A>(Structs: TupleSchema<[A]>): Struct<A, null> |
168 |
| -export function intersection<A, B>( |
169 |
| - Structs: TupleSchema<[A, B]> |
170 |
| -): Struct<A & B, null> |
171 |
| -export function intersection<A, B, C>( |
172 |
| - Structs: TupleSchema<[A, B, C]> |
173 |
| -): Struct<A & B & C, null> |
174 |
| -export function intersection<A, B, C, D>( |
175 |
| - Structs: TupleSchema<[A, B, C, D]> |
176 |
| -): Struct<A & B & C & D, null> |
177 |
| -export function intersection<A, B, C, D, E>( |
178 |
| - Structs: TupleSchema<[A, B, C, D, E]> |
179 |
| -): Struct<A & B & C & D & E, null> |
180 |
| -export function intersection<A, B, C, D, E, F>( |
181 |
| - Structs: TupleSchema<[A, B, C, D, E, F]> |
182 |
| -): Struct<A & B & C & D & E & F, null> |
183 |
| -export function intersection<A, B, C, D, E, F, G>( |
184 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G]> |
185 |
| -): Struct<A & B & C & D & E & F & G, null> |
186 |
| -export function intersection<A, B, C, D, E, F, G, H>( |
187 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H]> |
188 |
| -): Struct<A & B & C & D & E & F & G & H, null> |
189 |
| -export function intersection<A, B, C, D, E, F, G, H, I>( |
190 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I]> |
191 |
| -): Struct<A & B & C & D & E & F & G & H & I, null> |
192 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J>( |
193 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J]> |
194 |
| -): Struct<A & B & C & D & E & F & G & H & I & J, null> |
195 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K>( |
196 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K]> |
197 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K, null> |
198 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L>( |
199 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L]> |
200 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K & L, null> |
201 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L, M>( |
202 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M]> |
203 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K & L & M, null> |
204 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L, M, N>( |
205 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N]> |
206 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K & L & M & N, null> |
207 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O>( |
208 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]> |
209 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K & L & M & N & O, null> |
210 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>( |
211 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P]> |
212 |
| -): Struct<A & B & C & D & E & F & G & H & I & J & K & L & M & N & O & P, null> |
213 |
| -export function intersection<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q>( |
214 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q]> |
215 |
| -): Struct< |
216 |
| - A & B & C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q, |
217 |
| - null |
218 |
| -> |
219 |
| -export function intersection(Structs: Array<Struct<any, any>>): any { |
| 169 | +export function intersection<A extends AnyStruct, B extends AnyStruct[]>( |
| 170 | + Structs: [A, ...B] |
| 171 | +): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, null> { |
220 | 172 | return new Struct({
|
221 | 173 | type: 'intersection',
|
222 | 174 | schema: null,
|
@@ -476,65 +428,20 @@ export function string(): Struct<string, null> {
|
476 | 428 | * elements is of a specific type.
|
477 | 429 | */
|
478 | 430 |
|
479 |
| -export function tuple<A>(Structs: TupleSchema<[A]>): Struct<[A], null> |
480 |
| -export function tuple<A, B>(Structs: TupleSchema<[A, B]>): Struct<[A, B], null> |
481 |
| -export function tuple<A, B, C>( |
482 |
| - Structs: TupleSchema<[A, B, C]> |
483 |
| -): Struct<[A, B, C], null> |
484 |
| -export function tuple<A, B, C, D>( |
485 |
| - Structs: TupleSchema<[A, B, C, D]> |
486 |
| -): Struct<[A, B, C, D], null> |
487 |
| -export function tuple<A, B, C, D, E>( |
488 |
| - Structs: TupleSchema<[A, B, C, D, E]> |
489 |
| -): Struct<[A, B, C, D, E], null> |
490 |
| -export function tuple<A, B, C, D, E, F>( |
491 |
| - Structs: TupleSchema<[A, B, C, D, E, F]> |
492 |
| -): Struct<[A, B, C, D, E, F], null> |
493 |
| -export function tuple<A, B, C, D, E, F, G>( |
494 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G]> |
495 |
| -): Struct<[A, B, C, D, E, F, G], null> |
496 |
| -export function tuple<A, B, C, D, E, F, G, H>( |
497 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H]> |
498 |
| -): Struct<[A, B, C, D, E, F, G, H], null> |
499 |
| -export function tuple<A, B, C, D, E, F, G, H, I>( |
500 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I]> |
501 |
| -): Struct<[A, B, C, D, E, F, G, H, I], null> |
502 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J>( |
503 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J]> |
504 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J], null> |
505 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K>( |
506 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K]> |
507 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K], null> |
508 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L>( |
509 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L]> |
510 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L], null> |
511 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L, M>( |
512 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M]> |
513 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L, M], null> |
514 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L, M, N>( |
515 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N]> |
516 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L, M, N], null> |
517 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O>( |
518 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]> |
519 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O], null> |
520 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>( |
521 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P]> |
522 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P], null> |
523 |
| -export function tuple<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q>( |
524 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q]> |
525 |
| -): Struct<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q], null> |
526 |
| -export function tuple(Elements: Struct<any>[]): any { |
| 431 | +export function tuple<A extends AnyStruct, B extends AnyStruct[]>( |
| 432 | + Structs: [A, ...B] |
| 433 | +): Struct<[Infer<A>, ...InferStructTuple<B>], null> { |
527 | 434 | const Never = never()
|
528 | 435 |
|
529 | 436 | return new Struct({
|
530 | 437 | type: 'tuple',
|
531 | 438 | schema: null,
|
532 | 439 | *entries(value) {
|
533 | 440 | if (Array.isArray(value)) {
|
534 |
| - const length = Math.max(Elements.length, value.length) |
| 441 | + const length = Math.max(Structs.length, value.length) |
535 | 442 |
|
536 | 443 | for (let i = 0; i < length; i++) {
|
537 |
| - yield [i, value[i], Elements[i] || Never] |
| 444 | + yield [i, value[i], Structs[i] || Never] |
538 | 445 | }
|
539 | 446 | }
|
540 | 447 | },
|
@@ -580,57 +487,9 @@ export function type<S extends ObjectSchema>(
|
580 | 487 | * Ensure that a value matches one of a set of types.
|
581 | 488 | */
|
582 | 489 |
|
583 |
| -export function union<A>(Structs: TupleSchema<[A]>): Struct<A, null> |
584 |
| -export function union<A, B>(Structs: TupleSchema<[A, B]>): Struct<A | B, null> |
585 |
| -export function union<A, B, C>( |
586 |
| - Structs: TupleSchema<[A, B, C]> |
587 |
| -): Struct<A | B | C, null> |
588 |
| -export function union<A, B, C, D>( |
589 |
| - Structs: TupleSchema<[A, B, C, D]> |
590 |
| -): Struct<A | B | C | D, null> |
591 |
| -export function union<A, B, C, D, E>( |
592 |
| - Structs: TupleSchema<[A, B, C, D, E]> |
593 |
| -): Struct<A | B | C | D | E, null> |
594 |
| -export function union<A, B, C, D, E, F>( |
595 |
| - Structs: TupleSchema<[A, B, C, D, E, F]> |
596 |
| -): Struct<A | B | C | D | E | F, null> |
597 |
| -export function union<A, B, C, D, E, F, G>( |
598 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G]> |
599 |
| -): Struct<A | B | C | D | E | F | G, null> |
600 |
| -export function union<A, B, C, D, E, F, G, H>( |
601 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H]> |
602 |
| -): Struct<A | B | C | D | E | F | G | H, null> |
603 |
| -export function union<A, B, C, D, E, F, G, H, I>( |
604 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I]> |
605 |
| -): Struct<A | B | C | D | E | F | G | H | I, null> |
606 |
| -export function union<A, B, C, D, E, F, G, H, I, J>( |
607 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J]> |
608 |
| -): Struct<A | B | C | D | E | F | G | H | I | J, null> |
609 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K>( |
610 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K]> |
611 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K, null> |
612 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L>( |
613 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L]> |
614 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K | L, null> |
615 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L, M>( |
616 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M]> |
617 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K | L | M, null> |
618 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L, M, N>( |
619 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N]> |
620 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K | L | M | N, null> |
621 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O>( |
622 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]> |
623 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K | L | M | N | O, null> |
624 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P>( |
625 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P]> |
626 |
| -): Struct<A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P, null> |
627 |
| -export function union<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q>( |
628 |
| - Structs: TupleSchema<[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q]> |
629 |
| -): Struct< |
630 |
| - A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q, |
631 |
| - null |
632 |
| -> |
633 |
| -export function union(Structs: Struct<any>[]): any { |
| 490 | +export function union<A extends AnyStruct, B extends AnyStruct[]>( |
| 491 | + Structs: [A, ...B] |
| 492 | +): Struct<Infer<A> | InferStructTuple<B>[number], null> { |
634 | 493 | const description = Structs.map((s) => s.type).join(' | ')
|
635 | 494 | return new Struct({
|
636 | 495 | type: 'union',
|
|
0 commit comments