You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
In our codebase we use fishery a lot and love it. But we also love strict types and are now having an issue with fishery and a utility type we use, NonEmptyArray. A very short way to reproduce this is:
// Utility type
type NonEmptyArray<T extends any> = [T, ...T[]]
// Domain object types
type Bar = {
baz: number
}
type Foo = {
bars: NonEmptyArray<Bar>
}
// Factories
const barFactory = Factory.define<Bar>(() => ({
baz: 1
}))
const fooFactory = Factory.define<Foo>(() => ({
bars: barFactory.buildList(3) // TS2322: Type 'Bar[]' is not assignable to type 'NonEmptyArray<Bar>'. Source provides no match for required element at position 0 in target.
}))
Describe the solution you'd like
The definitions of buildList and createList could have function overloads to state that
if the number of items is 0, the return type is never[] (never[] can always be assigned to T[], link to playground)
if the number is not 0, the output type is [T, ...T[]]
Describe alternatives you've considered
I know this is not the biggest issue as it can be bypassed by just replacing fooFactory.buildList(5) with [fooFactory.build(), fooFactory.build(), fooFactory.build(), fooFactory.build(), fooFactory.build()]. But doing this really sucks, and I don't see any downsides with the overloads so I'll open a PR about it
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
In our codebase we use
fishery
a lot and love it. But we also love strict types and are now having an issue withfishery
and a utility type we use,NonEmptyArray
. A very short way to reproduce this is:Describe the solution you'd like
The definitions of
buildList
andcreateList
could have function overloads to state thatnever[]
(never[]
can always be assigned toT[]
, link to playground)[T, ...T[]]
Describe alternatives you've considered
I know this is not the biggest issue as it can be bypassed by just replacing
fooFactory.buildList(5)
with[fooFactory.build(), fooFactory.build(), fooFactory.build(), fooFactory.build(), fooFactory.build()]
. But doing this really sucks, and I don't see any downsides with the overloads so I'll open a PR about itThe text was updated successfully, but these errors were encountered: