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

buildList and createList typing doesn't outrule empty array from results #95

Open
aapzu opened this issue Mar 20, 2022 · 1 comment
Open

Comments

@aapzu
Copy link

aapzu commented Mar 20, 2022

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

@aapzu
Copy link
Author

aapzu commented Mar 20, 2022

Opened #96

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

No branches or pull requests

1 participant