Skip to content

Commit 2d8681e

Browse files
committed
fix: proper types for more complex cases of array
Fixes #3086
1 parent 0ffadef commit 2d8681e

File tree

2 files changed

+93
-32
lines changed

2 files changed

+93
-32
lines changed

lib/index.d.ts

Lines changed: 82 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,12 @@ declare namespace Joi {
17051705

17061706
type ComparatorFunction = (a: any, b: any) => boolean;
17071707

1708+
type UnwrapSchemaLikeWithoutArray<T> = T extends Joi.SchemaLikeWithoutArray<
1709+
infer U
1710+
>
1711+
? U
1712+
: never;
1713+
17081714
interface ArraySchema<TSchema = any[]> extends AnySchema<TSchema> {
17091715
/**
17101716
* Verifies that an assertion passes for at least one item in the array, where:
@@ -1723,38 +1729,82 @@ declare namespace Joi {
17231729
*
17241730
* @param type - a joi schema object to validate each array item against.
17251731
*/
1726-
items<A>(a: SchemaLikeWithoutArray<A>): ArraySchema<A[]>;
1727-
items<A, B>(
1728-
a: SchemaLikeWithoutArray<A>,
1729-
b: SchemaLikeWithoutArray<B>
1730-
): ArraySchema<(A | B)[]>;
1731-
items<A, B, C>(
1732-
a: SchemaLikeWithoutArray<A>,
1733-
b: SchemaLikeWithoutArray<B>,
1734-
c: SchemaLikeWithoutArray<C>
1735-
): ArraySchema<(A | B | C)[]>;
1736-
items<A, B, C, D>(
1737-
a: SchemaLikeWithoutArray<A>,
1738-
b: SchemaLikeWithoutArray<B>,
1739-
c: SchemaLikeWithoutArray<C>,
1740-
d: SchemaLikeWithoutArray<D>
1741-
): ArraySchema<(A | B | C | D)[]>;
1742-
items<A, B, C, D, E>(
1743-
a: SchemaLikeWithoutArray<A>,
1744-
b: SchemaLikeWithoutArray<B>,
1745-
c: SchemaLikeWithoutArray<C>,
1746-
d: SchemaLikeWithoutArray<D>,
1747-
e: SchemaLikeWithoutArray<E>
1748-
): ArraySchema<(A | B | C | D | E)[]>;
1749-
items<A, B, C, D, E, F>(
1750-
a: SchemaLikeWithoutArray<A>,
1751-
b: SchemaLikeWithoutArray<B>,
1752-
c: SchemaLikeWithoutArray<C>,
1753-
d: SchemaLikeWithoutArray<D>,
1754-
e: SchemaLikeWithoutArray<E>,
1755-
f: SchemaLikeWithoutArray<F>
1756-
): ArraySchema<(A | B | C | D | E | F)[]>;
1757-
items<TItems>(...types: SchemaLikeWithoutArray<TItems>[]): this;
1732+
items<A, TA = SchemaLikeWithoutArray<A>>(
1733+
a: TA
1734+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA>[]>;
1735+
items<A, B, TA = SchemaLikeWithoutArray<A>, TB = SchemaLikeWithoutArray<B>>(
1736+
a: TA,
1737+
b: TB
1738+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA | TB>[]>;
1739+
items<
1740+
A,
1741+
B,
1742+
C,
1743+
TA = SchemaLikeWithoutArray<A>,
1744+
TB = SchemaLikeWithoutArray<B>,
1745+
TC = SchemaLikeWithoutArray<C>
1746+
>(
1747+
a: TA,
1748+
b: TB,
1749+
c: TC
1750+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA | TB | TC>[]>;
1751+
items<
1752+
A,
1753+
B,
1754+
C,
1755+
D,
1756+
TA = SchemaLikeWithoutArray<A>,
1757+
TB = SchemaLikeWithoutArray<B>,
1758+
TC = SchemaLikeWithoutArray<C>,
1759+
TD = SchemaLikeWithoutArray<D>
1760+
>(
1761+
a: TA,
1762+
b: TB,
1763+
c: TC,
1764+
d: TD
1765+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA | TB | TC | TD>[]>;
1766+
items<
1767+
A,
1768+
B,
1769+
C,
1770+
D,
1771+
E,
1772+
TA = SchemaLikeWithoutArray<A>,
1773+
TB = SchemaLikeWithoutArray<B>,
1774+
TC = SchemaLikeWithoutArray<C>,
1775+
TD = SchemaLikeWithoutArray<D>,
1776+
TE = SchemaLikeWithoutArray<E>
1777+
>(
1778+
a: TA,
1779+
b: TB,
1780+
c: TC,
1781+
d: TD,
1782+
e: TE
1783+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA | TB | TC | TD | TE>[]>;
1784+
items<
1785+
A,
1786+
B,
1787+
C,
1788+
D,
1789+
E,
1790+
F,
1791+
TA = SchemaLikeWithoutArray<A>,
1792+
TB = SchemaLikeWithoutArray<B>,
1793+
TC = SchemaLikeWithoutArray<C>,
1794+
TD = SchemaLikeWithoutArray<D>,
1795+
TE = SchemaLikeWithoutArray<E>,
1796+
TF = SchemaLikeWithoutArray<F>
1797+
>(
1798+
a: TA,
1799+
b: TB,
1800+
c: TC,
1801+
d: TD,
1802+
e: TE,
1803+
f: TF
1804+
): ArraySchema<UnwrapSchemaLikeWithoutArray<TA | TB | TC | TD | TE | TF>[]>;
1805+
items<TItems extends any[]>(
1806+
...types: { [I in keyof TItems]: TItems[I] }
1807+
): ArraySchema<{ [I in keyof TItems]: UnwrapSchemaLikeWithoutArray<TItems[I]> }[number][]>;
17581808

17591809
/**
17601810
* Specifies the exact number of items in the array.

test/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,17 @@ expect.type<Joi.ArraySchema<(string | number | boolean | {key: string})[]>>(valu
403403

404404
expect.type<Joi.ArraySchema<boolean[]>>(Joi.array().items(Joi.boolean()));
405405
expect.type<Joi.ArraySchema<number[][]>>(Joi.array().items(Joi.array().items(Joi.number())));
406+
expect.type<Joi.ArraySchema<(string | number)[]>(Joi.array().items(process.env.NODE_ENV ? Joi.string() : Joi.number()));
407+
expect.type<Joi.ArraySchema<(string | number | boolean | Date)[]>(Joi.array().items(process.env.NODE_ENV ? Joi.string() : Joi.number(), process.env.NODE_ENV ? Joi.boolean() : Joi.date()));
408+
const arraySchema = Joi.array().items(
409+
Joi.binary(),
410+
Joi.boolean(),
411+
Joi.date(),
412+
Joi.function(),
413+
Joi.number(),
414+
Joi.object<Record<string,string>>(),
415+
Joi.string(),
416+
);
406417

407418
// - - - - - - - -
408419

0 commit comments

Comments
 (0)