Skip to content

Commit

Permalink
🏷️ Better typings for constant (#4999)
Browse files Browse the repository at this point in the history
**Description**

As recommended by #4047,
let's leverage const generics to make usage of `constant` even simpler
for TS users.

Not really a breaking change on its own but given we play with types and
change the way we declare some, let's be safe and pack this change with
other "next major" devs.

<!-- Please provide a short description and potentially linked issues
hustifying the need for this PR -->

<!-- * Your PR is fixing a bug or regression? Check for existing issues
related to this bug and link them -->
<!-- * Your PR is adding a new feature? Make sure there is a related
issue or discussion attached to it -->

<!-- You can provide any additional context to help into understanding
what's this PR is attempting to solve: reproduction of a bug, code
snippets... -->

**Checklist** — _Don't delete this checklist and make sure you do the
following before opening the PR_

- [x] The name of my PR follows [gitmoji](https://gitmoji.dev/)
specification
- [x] My PR references one of several related issues (if any)
- [x] New features or breaking changes must come with an associated
Issue or Discussion
- [x] My PR does not add any new dependency without an associated Issue
or Discussion
- [x] My PR includes bumps details, please run `yarn bump` and flag the
impacts properly
- [x] My PR adds relevant tests and they would have failed without my PR
(when applicable)

<!-- More about contributing at
https://github.com/dubzzz/fast-check/blob/main/CONTRIBUTING.md -->

**Advanced**

<!-- How to fill the advanced section is detailed below! -->

- [x] Category: 🏷️ Add or update types
- [x] Impacts: Typings

<!-- [Category] Please use one of the categories below, it will help us
into better understanding the urgency of the PR -->
<!-- * ✨ Introduce new features -->
<!-- * 📝 Add or update documentation -->
<!-- * ✅ Add or update tests -->
<!-- * 🐛 Fix a bug -->
<!-- * 🏷️ Add or update types -->
<!-- * ⚡️ Improve performance -->
<!-- * _Other(s):_ ... -->

<!-- [Impacts] Please provide a comma separated list of the potential
impacts that might be introduced by this change -->
<!-- * Generated values: Can your change impact any of the existing
generators in terms of generated values, if so which ones? when? -->
<!-- * Shrink values: Can your change impact any of the existing
generators in terms of shrink values, if so which ones? when? -->
<!-- * Performance: Can it require some typings changes on user side?
Please give more details -->
<!-- * Typings: Is there a potential performance impact? In which cases?
-->
  • Loading branch information
dubzzz authored May 29, 2024
1 parent 23bc19b commit 2fdf083
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/fast-check/src/arbitrary/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import { ConstantArbitrary } from './_internals/ConstantArbitrary';
* @remarks Since 0.0.1
* @public
*/
export function constant<T>(value: T): Arbitrary<T> {
export function constant<const T>(value: T): Arbitrary<T> {
return new ConstantArbitrary([value]);
}
4 changes: 2 additions & 2 deletions packages/fast-check/test/e2e/RecursiveStructures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe(`RecursiveStructures (seed: ${seed})`, () => {
it('Should shrink memo/oneof towards the smallest case (on very simple scenario)', () => {
// Arrange
const failingLength = 2;
const dataArb: fc.Memo<unknown[]> = fc.memo((n) => {
const dataArb: fc.Memo<readonly unknown[]> = fc.memo((n) => {
if (n <= 1) return fc.constant([0]);
else return fc.oneof({ withCrossShrink: true }, fc.constant([0]), fc.tuple(dataArb(), dataArb()));
});
Expand Down Expand Up @@ -125,7 +125,7 @@ describe(`RecursiveStructures (seed: ${seed})`, () => {

// Helpers

function flat(arr: unknown[]): unknown[] {
function flat(arr: readonly unknown[]): unknown[] {
return arr.reduce((acc: unknown[], cur: unknown) => {
if (Array.isArray(cur)) acc.push(...flat(cur));
else acc.push(cur);
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-check/test/unit/arbitrary/webUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('webUrl (integration)', () => {
function webUrlConstraintsBuilder(onlySmall?: boolean): fc.Arbitrary<WebUrlConstraints> {
return fc.record(
{
validSchemes: fc.constant(['ftp']),
validSchemes: fc.constant<string[]>(['ftp']),
authoritySettings: fc.record(
{
withIPv4: fc.boolean(),
Expand Down

0 comments on commit 2fdf083

Please sign in to comment.