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

Storybook: Use satisfies operator to get helpful errors about missing props #90570

Merged
merged 1 commit into from May 15, 2024

Conversation

oswian
Copy link
Contributor

@oswian oswian commented May 10, 2024

Proposed Changes

As we expand our usage of Storybook and our components continue to evolve, it's important that we render components within Storybook with the correct props to avoid inadvertently breaking stories or introducing bugs.

Currently, if we add a new required prop or rename an existing required prop in a component, and we don't update the corresponding stories, the TypeScript compiler won't raise any errors. This can lead to stories being in an incorrect state, which can be difficult to catch.

This change adopts the TS satisfies operator to address this issue. It allows us to ensure that the props passed to a component in a story conform to the expected shape defined by the component's props interface. If a story is missing a required prop or has an incorrect prop, the TypeScript compiler will now catch the issue and provide a clear error message.

Eg. we add a new required prop:

CleanShot 2024-05-13 at 17 30 54@2x

TS will now helpfully detect that we haven't yet added this prop to our story:

CleanShot 2024-05-13 at 17 16 00@2x

CleanShot 2024-05-13 at 17 23 51@2x

Testing Instructions

Try adding a new required prop to FeaturesGridProps or ComparisonGridProps (or rename an existing required prop) and check that you can see a TS warning in the relevant .stories.tsx files.

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • https://wpcalypso.wordpress.com/devdocs/docs/testing/index.md for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

@matticbot
Copy link
Contributor

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@oswian oswian force-pushed the update/stricter-types-for-storybook branch from 8a038ea to 36ee397 Compare May 13, 2024 06:39
@oswian oswian changed the title [WIP] Update our general approach to types in Storybook Storybook: Use satisfies operator to provide helpful errors about missing props May 13, 2024
@oswian oswian marked this pull request as ready for review May 13, 2024 07:53
@oswian oswian requested a review from a team as a code owner May 13, 2024 07:53
@oswian oswian requested a review from jeyip May 13, 2024 07:53
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label May 13, 2024
@oswian oswian changed the title Storybook: Use satisfies operator to provide helpful errors about missing props Storybook: Use satisfies operator to get helpful errors about missing props May 13, 2024
@oswian oswian self-assigned this May 14, 2024
Copy link
Contributor

@ddc22 ddc22 left a comment

Choose a reason for hiding this comment

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

TIL :shipit:

Comment on lines +76 to +85
const meta = {
title: 'ComparisonGrid',
component: ComponentWrapper,
decorators: [
( Story, { args: { trailMapVariant } } ) => {
trailMapVariant && setTrailMapExperiment( trailMapVariant );
return <Story />;
},
],
} satisfies Meta< typeof ComponentWrapper >;
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting TIL!

The docs explains it as a way to enforce the type
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html

I was curious and did some playing around with this feature
image.

The error is more prominent!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sharing this post which I found helpful: https://www.totaltypescript.com/clarifying-the-satisfies-operator

"When you use a colon, the type BEATS the value ... When you use satisfies, the value BEATS the type"

So in your example if we make newProp optional, satisfies narrows the type and provides a nice warning:

CleanShot 2024-05-15 at 13 24 07@2x

@oswian
Copy link
Contributor Author

oswian commented May 15, 2024

@ddc22 Thanks for the review!

@oswian oswian merged commit fa5d6fc into trunk May 15, 2024
41 of 44 checks passed
@oswian oswian deleted the update/stricter-types-for-storybook branch May 15, 2024 03:30
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants