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

Improve actions type #7012

Merged
merged 2 commits into from
Jul 2, 2019
Merged

Conversation

matt-tingen
Copy link
Contributor

@matt-tingen matt-tingen commented Jun 8, 2019

Issue:

This fixes the issue where spreading actions('onChange', 'onSubmit') into a component which requires onChange and onSubmit props would still result in an error requesting those props be specified. I did not see an existing issue which describes this.

What I did

This improves the actions type definition to return an object with the specific handler names provided. This is achieved via generic overloads.

With this change, the return type of the above call would be Record<'onChange' | 'onSubmit', ActionHandler> and can fulfill required props.

The overloads are a bit awkward because the trailing optional options param cannot follow a rest param.

How to test

Inspect the return type of calls to actions of the form actions('a', 'b', {}), actions({ a: 'a', b: 'b' }, {}), etc to ensure they return an object with specific keys rather than an indexed object. Alternatively, attempt to spread the return value of actions into e.g. a React component with required handler props.

  • Is this testable with Jest or Chromatic screenshots? No
  • Does this need a new example in the kitchen sink apps? No
  • Does this need an update to the documentation? No

If they need to be expanded, the `ActionsFunction` overloads can be
generated with the following code:

```js
function buildActionsFactoryOverloads(numParams) {
  const overloads = [];

  for (let n = 0; n < numParams; n++) {
    const handlerParams = [];
    for (let i = 1; i <= n + 1; i++) {
      handlerParams.push(`handler${i}: T`);
    }
    overloads.push(
      `<T extends string>(${handlerParams.join(
        ', ',
      )}, options?: ActionOptions): ActionsMap<T>;`,
    );
  }

  return overloads.join('\n');
}

console.log(buildActionsFactoryOverloads(10));
```
@vercel
Copy link

vercel bot commented Jun 8, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

Latest deployment for this branch: https://monorepo-git-fork-matt-tingen-actions-type.storybook.now.sh

@shilman
Copy link
Member

shilman commented Jul 2, 2019

@gaetanmaisse can you review & merge if it looks good?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: actions maintenance User-facing maintenance tasks typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants