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

Allow specifying the type of params #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

Dimrok
Copy link

@Dimrok Dimrok commented Oct 17, 2022

Rational

Like #94 with Date, I faced a problem with some "complex" types I wanted to pass as params.

The Pull Request allows one to specify the expected type of params (initially DeepPartial<T>) so we can easily use types such as luxon's DateTime without altering the library (unlike #94).

Note: For backward compatibility, P (type of params) was of course defaulted to DeepPartial<T>.

Example

Before

The library used to prevent this:

import { DateTime } from 'luxon';

type Event = {
  id: string;
  createdAt: DateTime;
}

export class EventFactory extends Factory<Event> {}

export const eventFactory = EventFactory.define(({ params }) => {
  const createdAt = createdAt ?? DateTime.fromJSDate(faker.date.soon());

  return {
    id: '1',
    createdAt,
  };
});

params#createdAt was a DeepPartial<DateTime>, which prevent the pattern const createdAt = createdAt ?? DateTime.fromJSDate(faker.date.soon());

After

import { DateTime } from 'luxon';

type Event = {
  id: string;
  createdAt: DateTime;
}

type EventParams = DeepPartial<Event> & {
  createdAt?: DateTime;
};  

export class EventFactory extends Factory<Event, {}, Event, EventParams> {}

export const eventFactory = EventFactory.define(({ params }) => {
  const createdAt = createdAt ?? DateTime.fromJSDate(faker.date.soon());

  return {
    id: '1',
    createdAt,
  };
});

Signed-off-by: Antony Méchin <[email protected]>
@filipkis
Copy link

It would be really great to get this merged as we're stumbling on this issue now that I'm running out of ideas on how to solve since we use rather complex types.

@Mange
Copy link

Mange commented Sep 20, 2024

This looks very sensible to me.

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

Successfully merging this pull request may close these issues.

3 participants