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

Addon-knobs: improve types via generics and readonlyarray #7411

Conversation

emilio-martinez
Copy link
Contributor

@emilio-martinez emilio-martinez commented Jul 13, 2019

Issue: Needed to add more fidelity to the knobs value types to cover even more test cases, including ReadonlyArray cases (#7348) and enum cases (#7420).

What I did

  • Added more fidelity to the knobs value types, particularly where many different kinds of values are allowed.
  • Added types to cover cases where a ReadonlyArray may be provided (Typescript seems to treat this differently as a normal array)
  • Added a ts file with various use cases as a form of "tests" (would love to get feedback on approach)

How to test

The knobs addon spans a fair number of use cases; therefore, I had the need to create a set of test cases with which I want to ensure compilation passes. To this effect, I added a file under src/__types__/ with a series of test cases. The successful compilation of this file should indicate types are passing correctly, at least as far as that file is intending to document.

I don't believe there's currently an established process for testing type definitions specifically in the repo, so I'd love to get thoughts/feedback on the approach.

@vercel
Copy link

vercel bot commented Jul 13, 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-emilio-martinez-fix-addon-knobs-d58aac.storybook.now.sh

Copy link
Member

@CodeByAlex CodeByAlex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice fix! I like your approach to testing the types 👍

@CodeByAlex
Copy link
Member

@shilman any chance we could get this one in sooner rather than later. It's a blocker to upgrade to a version higher than ~.35

@emilio-martinez emilio-martinez force-pushed the fix/addon-knobs-generics-and-readonlyarray branch from 51ae10e to d3d6548 Compare July 17, 2019 09:59
@vercel vercel bot temporarily deployed to staging July 17, 2019 09:59 Inactive
@emilio-martinez emilio-martinez force-pushed the fix/addon-knobs-generics-and-readonlyarray branch from d3d6548 to 98265c7 Compare July 17, 2019 10:07
@shilman shilman merged commit 291bf23 into storybookjs:next Jul 18, 2019
@shilman shilman added this to the 5.2.0 milestone Jul 18, 2019
@CodeByAlex
Copy link
Member

@emilio-martinez, is this working for you in your project? I'm getting the following errors in mine:

ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Array.d.ts
ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Array.d.ts(3,62):
TS2693: 'string' only refers to a type, but is being used as a value here.

ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts
ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts(4,134):
TS2304: Cannot find name 'readonly'.

ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts
ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts(4,143):
TS2365: Operator '>' cannot be applied to types 'boolean' and 'undefined[]'.

ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts
ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts(4,143):
TS2693: 'NonNullable' only refers to a type, but is being used as a value here.

ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts
ERROR in /node_modules/@storybook/addon-knobs/dist/components/types/Options.d.ts(4,155):
TS2304: Cannot find name 'T'.

@emilio-martinez
Copy link
Contributor Author

emilio-martinez commented Jul 22, 2019

@CodeByAlex haven't been getting that, but I'll ping you on Discord


type ArrayTypeKnobValue = string[];
export type ArrayTypeKnobValue = string[] | readonly string[];
Copy link

@nickofthyme nickofthyme Jul 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilio-martinez Is this breaking type change intentional? Using numbers will cause a typescript error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To quote the README for this add-on:

array
Allows you to get an array of strings from the user.

Unfortunately, the package from DefinitelyTyped is inaccurate—that field uses a text-based input of comma delimited values, so even if you were to pass a number array initially, it would subsequently return a string array on change, i.e., here's no number parsing logic for that control.

That said, I'd love to hear more about your use case and help in any way I can. Would you mind sharing?

Copy link

@nickofthyme nickofthyme Jul 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @emilio-martinez Thanks for the reply. Yeah it seems like you guys are just having to bite the bullet on these type changes going from javascript to typescript, which I totally feel your pain.

This is my use case 👇. It's fixable by just parsing the values and there is probably a better way to go about it. Just got this error when upgrading from v5.1.9 to v5.2.0-beta.19.
https://github.com/elastic/elastic-charts/blob/8ebe600df1ae476e25032ab1c7707de398143e43/stories/annotations.tsx#L41

I'm curious if you guys are gradually upgrading you codebase to ts or doing it in a single release (I'm too lazy to check for myself)? If it is in a single release why wouldn’t you just release ts in v6 rather than producing all these stricter type errors on a minor release?

Thanks 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS migration is an epic started at the end of 2018, we recently reached an important step as 50% of Storybook codebase in TS 🎉.
Migration progress is followed in #5030 😉. Avoiding breaking changes is one of our goals but we somethings found weird/wrong types/docs even in SB own codebase.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaetanmaisse Sweet congrats on the big 5-0!! 🎉 All the best keep up the great work.

Copy link
Contributor

@leoyli leoyli Jul 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there are many work around and we don't buy in strict type and noImplicitAny at day one (I think is for easier migration). For that reason, there are sheer amount of any flood in our typing. That somehow explains why we inevitably will have breaking changes in type system, we need to improve it gradually due to the fact that the codebase is pretty huge. But I think this brings more organization and community collaboration to make it awesome in long term.

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

Successfully merging this pull request may close these issues.

6 participants