Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback on oneof #546

Closed
gcanti opened this issue Feb 1, 2020 · 6 comments · Fixed by #3183
Closed

Feedback on oneof #546

gcanti opened this issue Feb 1, 2020 · 6 comments · Fixed by #3183

Comments

@gcanti
Copy link

gcanti commented Feb 1, 2020

💡 Idea

Currently oneof;

  1. forces the user to add a type annotation at the call site
  2. doesn't statically enforce at least one arb in input

Repros:

import * as fc from 'fast-check'

fc.oneof(fc.string(), fc.boolean()) // error
fc.oneof<string | boolean>(fc.string(), fc.boolean()) // ok
import * as fc from 'fast-check'

fc.oneof() // no error
fc.oneof(...[]) // no error

Not sure whether you may be interested (type level tricks are nice when they work, however they could add some maintenance burden) but here's a possible solution:

import * as fc from 'fast-check'

export declare function oneof<A extends [unknown, ...Array<unknown>]>(
  ...arbs: { [K in keyof A]: fc.Arbitrary<A[K]> }
): fc.Arbitrary<A[number]>

// 1)

// const arb: fc.Arbitrary<string | boolean>
const arb = oneof(fc.string(), fc.boolean()) // ok

// 2)

oneof() // error
oneof(...[]) // error
@dubzzz
Copy link
Owner

dubzzz commented Feb 3, 2020

Having better typings for oneof would definitely be a great thing.

The fact to avoid users to explicitly type <string, number> on fc.oneof(fc.nat(), fc. string()) would be pretty cool.

If you want to I can let you open a PR for that purpose. Otherwise I can handle it in the future.


Concerning the idea of forcing at least one parameter at call site, it was originally the case but the constraint has been removed by 1833a4b

I'll need to dig a little bit more to understand why. But I believe that it caused issues when combined with other arbitraries or chain:

fc.set(fc.uuid(), 1, 10)
  .chain(allUuids => fc.record({
    oneRecord: fc.oneof(...allUuids),
    records: fc.constant(allUuids),
  }))

Enforcing at least one parameter with typings might break existing code (that is working today) 🙄

@gcanti
Copy link
Author

gcanti commented Feb 28, 2020

Enforcing at least one parameter with typings might break existing code (that is working today)

Agree, technically is a breaking change

@dubzzz
Copy link
Owner

dubzzz commented Jun 23, 2020

As I'm planning a next major of fast-check (mostly to add a full support for esm), I'll maybe take advantage of it to include your request into it 👍

@jasikpark
Copy link
Contributor

I believe this issue has been resolved, right? I've had no problems w/ needing to specify .oneof generic types manually nowadays 👍

@dubzzz
Copy link
Owner

dubzzz commented Sep 11, 2022

It should not be that far indeed 🙂 But I think we still support the empty case from a typing point of view. If so it should not be that hard to forbid and finally close this old issue.

dubzzz added a commit that referenced this issue Sep 12, 2022
While ideally writing `oneof()` should be prevented having a version accepting `oneof(...arrayOfArbs)` but rejecting `oneof()` was not straightforward to come up with.

The current output for `oneof()` seems already to be already a good trade-off. So I believe we can close #546 for now.

Fixes #546
dubzzz added a commit that referenced this issue Sep 12, 2022
While ideally writing `oneof()` should be prevented having a version accepting `oneof(...arrayOfArbs)` but rejecting `oneof()` was not straightforward to come up with.

The current output for `oneof()` seems already to be already a good trade-off. So I believe we can close #546 for now.

Fixes #546
@jasikpark
Copy link
Contributor

woot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants