UNPKG

3.53 kBTypeScriptView Raw
1import type { Arbitrary } from '../check/arbitrary/definition/Arbitrary.js';
2import type { DepthIdentifier } from './_internals/helpers/DepthContext.js';
3import type { DepthSize } from './_internals/helpers/MaxLengthFromMinLength.js';
4/**
5 * Conjonction of a weight and an arbitrary used by {@link oneof}
6 * in order to generate values
7 *
8 * @remarks Since 1.18.0
9 * @public
10 */
11export interface WeightedArbitrary<T> {
12 /**
13 * Weight to be applied when selecting which arbitrary should be used
14 * @remarks Since 0.0.7
15 */
16 weight: number;
17 /**
18 * Instance of Arbitrary
19 * @remarks Since 0.0.7
20 */
21 arbitrary: Arbitrary<T>;
22}
23/**
24 * Either an `Arbitrary<T>` or a `WeightedArbitrary<T>`
25 * @remarks Since 3.0.0
26 * @public
27 */
28export type MaybeWeightedArbitrary<T> = Arbitrary<T> | WeightedArbitrary<T>;
29/**
30 * Infer the type of the Arbitrary produced by {@link oneof}
31 * given the type of the source arbitraries
32 *
33 * @remarks Since 2.2.0
34 * @public
35 */
36export type OneOfValue<Ts extends MaybeWeightedArbitrary<unknown>[]> = {
37 [K in keyof Ts]: Ts[K] extends MaybeWeightedArbitrary<infer U> ? U : never;
38}[number];
39/**
40 * Constraints to be applied on {@link oneof}
41 * @remarks Since 2.14.0
42 * @public
43 */
44export type OneOfConstraints = {
45 /**
46 * When set to true, the shrinker of oneof will try to check if the first arbitrary
47 * could have been used to discover an issue. It allows to shrink trees.
48 *
49 * Warning: First arbitrary must be the one resulting in the smallest structures
50 * for usages in deep tree-like structures.
51 *
52 * @defaultValue false
53 * @remarks Since 2.14.0
54 */
55 withCrossShrink?: boolean;
56 /**
57 * While going deeper and deeper within a recursive structure (see {@link letrec}),
58 * this factor will be used to increase the probability to generate instances
59 * of the first passed arbitrary.
60 *
61 * @remarks Since 2.14.0
62 */
63 depthSize?: DepthSize;
64 /**
65 * Maximal authorized depth.
66 * Once this depth has been reached only the first arbitrary will be used.
67 *
68 * @defaultValue Number.POSITIVE_INFINITY — _defaulting seen as "max non specified" when `defaultSizeToMaxWhenMaxSpecified=true`_
69 * @remarks Since 2.14.0
70 */
71 maxDepth?: number;
72 /**
73 * Depth identifier can be used to share the current depth between several instances.
74 *
75 * By default, if not specified, each instance of oneof will have its own depth.
76 * In other words: you can have depth=1 in one while you have depth=100 in another one.
77 *
78 * @remarks Since 2.14.0
79 */
80 depthIdentifier?: DepthIdentifier | string;
81};
82/**
83 * For one of the values generated by `...arbs` - with all `...arbs` equiprobable
84 *
85 * **WARNING**: It expects at least one arbitrary
86 *
87 * @param arbs - Arbitraries that might be called to produce a value
88 *
89 * @remarks Since 0.0.1
90 * @public
91 */
92declare function oneof<Ts extends MaybeWeightedArbitrary<unknown>[]>(...arbs: Ts): Arbitrary<OneOfValue<Ts>>;
93/**
94 * For one of the values generated by `...arbs` - with all `...arbs` equiprobable
95 *
96 * **WARNING**: It expects at least one arbitrary
97 *
98 * @param constraints - Constraints to be applied when generating the values
99 * @param arbs - Arbitraries that might be called to produce a value
100 *
101 * @remarks Since 2.14.0
102 * @public
103 */
104declare function oneof<Ts extends MaybeWeightedArbitrary<unknown>[]>(constraints: OneOfConstraints, ...arbs: Ts): Arbitrary<OneOfValue<Ts>>;
105export { oneof };