UNPKG

2.88 kBTypeScriptView Raw
1import type { Arbitrary } from '../check/arbitrary/definition/Arbitrary.js';
2/**
3 * Type of the value produced by {@link letrec}
4 * @remarks Since 3.0.0
5 * @public
6 */
7export type LetrecValue<T> = {
8 [K in keyof T]: Arbitrary<T[K]>;
9};
10/**
11 * Strongly typed type for the `tie` function passed by {@link letrec} to the `builder` function we pass to it.
12 * You may want also want to use its loosely typed version {@link LetrecLooselyTypedTie}.
13 *
14 * @remarks Since 3.0.0
15 * @public
16 */
17export interface LetrecTypedTie<T> {
18 <K extends keyof T>(key: K): Arbitrary<T[K]>;
19 (key: string): Arbitrary<unknown>;
20}
21/**
22 * Strongly typed type for the `builder` function passed to {@link letrec}.
23 * You may want also want to use its loosely typed version {@link LetrecLooselyTypedBuilder}.
24 *
25 * @remarks Since 3.0.0
26 * @public
27 */
28export type LetrecTypedBuilder<T> = (tie: LetrecTypedTie<T>) => LetrecValue<T>;
29/**
30 * Loosely typed type for the `tie` function passed by {@link letrec} to the `builder` function we pass to it.
31 * You may want also want to use its strongly typed version {@link LetrecTypedTie}.
32 *
33 * @remarks Since 3.0.0
34 * @public
35 */
36export type LetrecLooselyTypedTie = (key: string) => Arbitrary<unknown>;
37/**
38 * Loosely typed type for the `builder` function passed to {@link letrec}.
39 * You may want also want to use its strongly typed version {@link LetrecTypedBuilder}.
40 *
41 * @remarks Since 3.0.0
42 * @public
43 */
44export type LetrecLooselyTypedBuilder<T> = (tie: LetrecLooselyTypedTie) => LetrecValue<T>;
45/**
46 * For mutually recursive types
47 *
48 * @example
49 * ```typescript
50 * type Leaf = number;
51 * type Node = [Tree, Tree];
52 * type Tree = Node | Leaf;
53 * const { tree } = fc.letrec<{ tree: Tree, node: Node, leaf: Leaf }>(tie => ({
54 * tree: fc.oneof({depthSize: 'small'}, tie('leaf'), tie('node')),
55 * node: fc.tuple(tie('tree'), tie('tree')),
56 * leaf: fc.nat()
57 * }));
58 * // tree is 50% of node, 50% of leaf
59 * // the ratio goes in favor of leaves as we go deeper in the tree (thanks to depthSize)
60 * ```
61 *
62 * @param builder - Arbitraries builder based on themselves (through `tie`)
63 *
64 * @remarks Since 1.16.0
65 * @public
66 */
67export declare function letrec<T>(builder: T extends Record<string, unknown> ? LetrecTypedBuilder<T> : never): LetrecValue<T>;
68/**
69 * For mutually recursive types
70 *
71 * @example
72 * ```typescript
73 * const { tree } = fc.letrec(tie => ({
74 * tree: fc.oneof({depthSize: 'small'}, tie('leaf'), tie('node')),
75 * node: fc.tuple(tie('tree'), tie('tree')),
76 * leaf: fc.nat()
77 * }));
78 * // tree is 50% of node, 50% of leaf
79 * // the ratio goes in favor of leaves as we go deeper in the tree (thanks to depthSize)
80 * ```
81 *
82 * @param builder - Arbitraries builder based on themselves (through `tie`)
83 *
84 * @remarks Since 1.16.0
85 * @public
86 */
87export declare function letrec<T>(builder: LetrecLooselyTypedBuilder<T>): LetrecValue<T>;