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

Why can toCollection return a non-array? #366

Open
dmitriybo opened this issue Sep 25, 2023 · 3 comments
Open

Why can toCollection return a non-array? #366

dmitriybo opened this issue Sep 25, 2023 · 3 comments

Comments

@dmitriybo
Copy link

dmitriybo commented Sep 25, 2023

Description

In my opinion, the toCollection method should always return an array (if it does not passed length, then length = 1). The reason for this is very simple: toCollection is used to generate an array of elements. If for some reason you think otherwise, then at least make the correct typing. Currently toCollection always returns the type Collection | Collection[], but should return Collection[] if length is passed.

image

The example in the specification also needs to be improved:

image

Proposed solution

No response

Alternatives considered

No response

Do you want to create a pull request?

No

@labeled
Copy link

labeled commented Nov 22, 2023

Came to post this same suggestion +1. toCollection to always return an array.

Suggestion Solution
toCollection() returns array with default length of 3

@tayambamwanza
Copy link

tayambamwanza commented May 5, 2024

Is anyone working on this? seems like a simple fix, I don't mind taking it on, just set the type to return array only right? I think it's ok to return an array of 1 by default even, what's the point of using this function to return just an object??

@predragnikolic
Copy link

The type here could just be improved to reflect the runtime behavior:

export function toCollection<
Collection = never,
Options extends FakeOptions = never
>(
generateCollection: (options?: Options) => Collection,
options?: Options
): Collection | Collection[] {

If options arg is passed with the length property, the return type should be an array, else it will be an object.
That can be achieved with the following code:

export declare function toCollection<Collection = never, Options extends FakeOptions = never>(generateCollection: (options?: Options) => Collection, options?: Options): Options extends {length: number} ? Collection[]: Collection;

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

5 participants