thoughtbot/fishery

DeepPartial does not support nullable Arrays

paulsouche opened this issue · 0 comments

Hello ! We ran recently in this issue regarding the following snippet

import { Factory } from 'fishery'

type Request = {
  queryParams: {
    [name: string]: string[] | undefined | null
  }
}

export const requestFactory = Factory.define<Request>(({ params }) => {
  const { queryParams = {} } = params

  return {
    queryParams,
  }
})

TypeScript will output an error

Argument of type '({ params }: GeneratorFnOptions<Request, any>) => { queryParams: DeepPartial<{ [name: string]: string[] | null | undefined; }>; }' is not assignable to parameter of type 'GeneratorFn<Request, any>'.
  Call signature return types '{ queryParams: DeepPartial<{ [name: string]: string[] | null | undefined; }>; }' and 'Request' are incompatible.
    The types of 'queryParams' are incompatible between these types.
      Type 'DeepPartial<{ [name: string]: string[] | null | undefined; }>' is not assignable to type '{ [name: string]: string[] | null | undefined; }'

This is because the DeepPartial type does not fallback correctly on null and undefined values

export type DeepPartial<T> = {
  [P in keyof T]?: unknown extends T[P]
    ? T[P]
    : T[P] extends Array<any>
    ? T[P]
    : DeepPartial<T[P]>;
};

Should be

export type DeepPartial<T> = {
  [P in keyof T]?: unknown extends T[P]
    ? T[P]
    : T[P] extends Array<any> | undefined | null
    ? T[P]
    : DeepPartial<T[P]>;
};

Thank you for the great job anyway