question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to chain records nicely (spreading, adding a property) / feature request for improvement

See original GitHub issue

💬 Question and Help

type A = {
  foo: boolean;
  bar: string;
  fuzz: number;
};

type B = A & {
  buzz: boolean;
};

const recordA = fc.record<A>({
  foo: fc.boolean(),
  bar: fc.string(),
  fuzz: fc.integer(),
});

const recordB: Arbitrary<B> = recordA.chain((a) =>
  fc.constantFrom({
    ...a,
    buzz: fc.boolean(),
  })
);

The above gives an indication of what I am trying to achieve, but doesn’t work (due to the fact that buzz will yield type error 'Arbitrary<boolean>' is not assignable to type 'boolean'.

I’ve solved it like this:

const recordB: Arbitrary<B> = fc
  .tuple(recordA, fc.boolean())
  .chain(([a, bool]) =>
    fc.constantFrom({
      ...a,
      buzz: bool,
    })
  );

but it doesn’t seem that pretty. I’m wondering if there is a better/recommended approach? It would be awesome if you could do something like:

const recordB: Arbitrary<B> = fc.record({ ...recordA, buzz: fc.boolean() });

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
waynevansoncommented, Apr 30, 2022

Here is a combinator for you, hope it helps.

const intersect =
  <
    A extends Record<string, unknown>,
    B extends Record<string,unknown>
  >(fa: Arbitrary<A>, fb: Arbitrary<B>): Arbitrary<A & B> =
    fc.tuple(fa, fb).map(([a,b]) => Object.assign({}, a, b))
1reaction
waynevansoncommented, May 6, 2022

The pattern I’d recommend is lifting your structures from A to Arbitrary<A> and composing it from there, like in the intersect example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Manage Feature Requests [Template included]
This guide will teach you everything about feature requests – how to process them, manage them, respond to them, prioritize them – so...
Read more >
7 Useful Tips to Manage Feature Requests - Craft.io
Feature requests can provide product managers with great ideas for product improvement, but they must be managed correctly. Here are our top tips....
Read more >
3 ways to manage software feature request - Amoeboids
Discover best practices to manage feature requests from different stakeholders. Practical tips to get you on top of the game.
Read more >
The Good-Better-Best Approach to Pricing
Key steps include identifying “fence” attributes that will prevent current customers from trading down from the existing offering; carefully choosing features ...
Read more >
Best Practices for EHR Implementation - Douglas County - Trilogy ...
Objective #3: Encourage continuous growth and learning among staff of the EHR system. How was practice implemented / activities To best prepare our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found