thoughtbot / fishery

A library for setting up JavaScript objects as test data
MIT License
876 stars 36 forks source link

`Date` params are hard to use with `DeepPartial` #93

Closed atomanyih closed 2 years ago

atomanyih commented 2 years ago

Description

If you have a factory for a type with a Date attribute, the param is typed as DeepPartial<Date> which you can't use to override default values.

It seems like Date should possibly be one of the exceptions defined in deepPartial.ts because it doesn't make sense to have it as a partial (similar to Set, Map, etc)

To Reproduce

sandbox: https://codesandbox.io/s/fishery-test-forked-g8rbsi

type User = {
  name: string;
  createdAt: Date;
};

const userFactory = Factory.define<User>(({ params }) => ({
  name: 'Susan',
  createdAt: params.createdAt || new Date(),
/*
Type 'Date | DeepPartial<Date>' is not assignable to type 'Date'.
  Type 'DeepPartial<Date>' is not assignable to type 'Date'.
    Types of property 'toString' are incompatible.
      Type 'DeepPartial<() => string> | undefined' is not assignable to type '() => string'.
        Type 'undefined' is not assignable to type '() => string'.ts(2322)
*/
}));

Additional context You can work around this by accessing createdAt or other Date attributes through associations, but this feels hacky

stevehanson commented 2 years ago

Thanks for opening this issue. This was fixed by #94 and released in v2.2.1. I will go ahead and close this, but if you are still seeing the issue, feel free to reopen.

I'm realizing, though, that this is an issue not just for Date but for any custom object with a constructor or prototype (eg. I still get the issue with DayJs). I'll open a separate issue for this.