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

Add type support for const/readonly arrays in .each cases #732

Closed
4 tasks done
IgnusG opened this issue Feb 11, 2022 · 1 comment · Fixed by #733
Closed
4 tasks done

Add type support for const/readonly arrays in .each cases #732

IgnusG opened this issue Feb 11, 2022 · 1 comment · Fixed by #733
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@IgnusG
Copy link
Contributor

IgnusG commented Feb 11, 2022

Clear and concise description of the problem

The current type specification does not allow const or readonly arrays so the following works but does not include proper types inside test:

test.each([
    [1, ["1"]],
    [2, ["1", "2"]],
    [3, ["1", "2", "3"]],
  ])("renders pagination for a total of %i pages", (total, expectedElements) => {
    // total is typed as number | string[]
    // expectedElements is typed as number | string[]
  });

The quick-fix marking the input array as readonly does not typecheck:

// however each does not accept readonly tuples
test.each([
    [1, ["1"]],
    [2, ["1", "2"]],
    [3, ["1", "2", "3"]],
  ] as const)("renders pagination for a total of %i pages", (total, expectedElements) => {
    // total would be typed as 1 | 2 | 3
    // expectedElements is typed as ["1"] | ["1", "2"] ...
  });

Suggested solution

The following change to the EachFunction type will do the trick:

declare type EachFunction = <T>(cases: T[] | readonly T[]) => (name: string, fn: (...args: T extends any[] | readonly any[] ? { -readonly [k in keyof T]: T[k] } : [T]) => void) => void;

Alternative

No response

Additional context

No response

Validations

@sheremet-va
Copy link
Member

PR is welcome 🙏

@sheremet-va sheremet-va added enhancement New feature or request good first issue Good for newcomers labels Feb 11, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants