thoughtbot / fishery

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

Fixes for DeepPartial type #78

Closed stevehanson closed 3 years ago

stevehanson commented 3 years ago

The DeepPartial type, which is used to define what can be passed as params to a factory, was not working properly with some types, notably indexed types and arrays that could be null or undefined.

Previously:

DeepPartial<{
  objectArray: { x: number }[];
  objectArrayNull: { x: number, y: number }[] | undefined | null;
  stringArrayNull: string[] | undefined | null;
  params: {
    [name: string]: string[] | undefined | null;
  };
}>

generated this type:

{
  objectArray?: { x: number;  }[] | undefined;
  objectArrayNull?: (DeepPartial<{ x: number; y: number  }> | undefined)[] | null | undefined;
  stringArrayNull?: (string | undefined)[] | null | undefined;
  params?: DeepPartial<{
    [name: string]: string[] | null | undefined;
  }> | undefined;
}

The most notable issues were:

The new DeepPartial generates:

{
  objectArray?: { x: number; }[] | undefined;
  objectArrayNull?: { x: number; }[] | undefined | null;
  stringArrayNull?: string[] | undefined | null;
  params?: DeepPartialObject<{
      [name: string]: string[] | null | undefined;
  }> | undefined;
}

This PR also adds tests that document and verify the new type.

Closes #67