Skip to content

Commit

Permalink
🏷️ Better typings for constantFrom (#4103)
Browse files Browse the repository at this point in the history
As recommended by #4047, let's leverage const generics to make usage of `constantFrom` even simpler for TS users.
  • Loading branch information
dubzzz committed May 17, 2024
1 parent 3500faa commit 221b787
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .yarn/versions/be2c76af.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
releases:
fast-check: minor

declined:
- "@fast-check/ava"
- "@fast-check/jest"
- "@fast-check/vitest"
- "@fast-check/worker"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { natToStringifiedNatMapper, natToStringifiedNatUnmapper } from '../mappe

/** @internal */
export function buildStringifiedNatArbitrary(maxValue: number): Arbitrary<string> {
return tuple(constantFrom<('dec' | 'oct' | 'hex')[]>('dec', 'oct', 'hex'), nat(maxValue)).map(
return tuple(constantFrom('dec', 'oct', 'hex'), nat(maxValue)).map(
natToStringifiedNatMapper,
natToStringifiedNatUnmapper,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-check/src/arbitrary/constantFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Elem<T> = T extends any[] ? T[number] : T;
* @remarks Since 0.0.12
* @public
*/
function constantFrom<T = never>(...values: T[]): Arbitrary<T>;
function constantFrom<const T = never>(...values: T[]): Arbitrary<T>;

/**
* For one `...values` values - all equiprobable
Expand Down
12 changes: 5 additions & 7 deletions packages/fast-check/test-types/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ expectType<fc.Arbitrary<string[]>>()(
fc.nat().chain((n) => fc.array(fc.char(), { maxLength: n })),
'Type of "chain" corresponds to the return type of the passed lambda',
);
expectType<fc.Arbitrary<number>>()(
expectType<fc.Arbitrary<1 | 2 | 3>>()(
fc.constantFrom(1, 2, 3).chain((value) => fc.constant(value)),
'Type of "chain" corresponds to the return type of the passed lambda',
);
Expand All @@ -110,21 +110,19 @@ expectType<fc.Arbitrary<string>>()(
);

// constantFrom arbitrary
expectType<fc.Arbitrary<number>>()(
fc.constantFrom(1, 2),
'By default, "constantFrom" simplifies the type (eg.: "1 -> number")',
);
expectType<fc.Arbitrary<1 | 2>>()(fc.constantFrom(1, 2), 'By default, "constantFrom" preserves the precise type');
expectType<fc.Arbitrary<number>>()(fc.constantFrom<number[]>(1, 2), 'But it also accepts to receive the type');
expectType<fc.Arbitrary<1 | 2>>()(
fc.constantFrom(...([1, 2] as const)),
'"as const" prevent extra simplification of "constantFrom"',
'"as const" was a way to prevent extra simplification of "constantFrom", it\'s now not needed anymore',
);
expectType<fc.Arbitrary<number | string>>()(
fc.constantFrom(1, 2, 'hello'),
'"constantFrom" accepts arguments not having the same types without any typing trick',
);
expectType<fc.Arbitrary<1 | 2 | 'hello'>>()(
fc.constantFrom(...([1, 2, 'hello'] as const)),
'"as const" prevent extra simplification of "constantFrom"',
'"as const" was a way to prevent extra simplification of "constantFrom"',
);

// uniqueArray arbitrary
Expand Down

0 comments on commit 221b787

Please sign in to comment.