dubzzz / fast-check

Property based testing framework for JavaScript (like QuickCheck) written in TypeScript
https://fast-check.dev/
MIT License
4.32k stars 179 forks source link

Make `fc.constantFrom` use `const` generics #4047

Open TomerAberbach opened 1 year ago

TomerAberbach commented 1 year ago

🚀 Feature Request

- declare function constantFrom<T = never>(...values: T[]): Arbitrary<T>;
+ declare function constantFrom<const T = never>(...values: T[]): Arbitrary<T>;

Motivation

Right now fc.constantFrom('a', 'b', 'c') returns Arbitrary<string>, which is a bit inconvenient because often the reason you are using fc.constantFrom in the first place is that you need the specific type represented by the constants. You can fix this by writing fc.constantFrom<'a' | 'b' | 'c'>('a', 'b', 'c'), but that requires duplicating the constants between the types and the values. const generics solve this problem.

Example

TS Playground

dubzzz commented 1 year ago

It might be a great thing to do for a next major 🤔 Doing that in the current one would mean break the official compatibility with TS and force users to bump to 5.0 or above. We can already create the PR and wait for a next major to be ready to merge it (I already have one queued PR with breaking).

TomerAberbach commented 1 year ago

Sounds good! Did you want me to make the PR? Or were you planning on doing it?

dubzzz commented 1 year ago

As you want.

Note: I think a next major will come soon even if it will not bring that much into fast-check: it will help me to change some internal (but exposed) APIs such as IProperty.

TomerAberbach commented 1 year ago

Thanks for moving ahead with this!

dubzzz commented 11 months ago

It will be part of v4. I'm currently stacking PRs to have it ready in the coming weeks/months. I might do some new minors in-between to properly advertise new APIs and deprecate old ones.

seansfkelley commented 7 months ago

This is a helpful change, thanks! Can you give the same treatment to constant?

dubzzz commented 5 months ago

@seansfkelley Just opened a PR with our suggestion for constant. Same thing as for constantFrom, it will have to wait v4. And this time I hope to be able to publish it soon (1-2 months is my target).

-> https://github.com/dubzzz/fast-check/pull/4999