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
Conversation
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. |
Jetpack Cloud live (direct link)
Automattic for Agencies live (direct link)
|
8a038ea
to
36ee397
Compare
satisfies
operator to provide helpful errors about missing props
satisfies
operator to provide helpful errors about missing propssatisfies
operator to get helpful errors about missing props
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL
const meta = { | ||
title: 'ComparisonGrid', | ||
component: ComponentWrapper, | ||
decorators: [ | ||
( Story, { args: { trailMapVariant } } ) => { | ||
trailMapVariant && setTrailMapExperiment( trailMapVariant ); | ||
return <Story />; | ||
}, | ||
], | ||
} satisfies Meta< typeof ComponentWrapper >; |
There was a problem hiding this comment.
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
.
The error is more prominent!
There was a problem hiding this comment.
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:
@ddc22 Thanks for the review! |
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:
TS will now helpfully detect that we haven't yet added this prop to our story:
Testing Instructions
Try adding a new required prop to
FeaturesGridProps
orComparisonGridProps
(or rename an existing required prop) and check that you can see a TS warning in the relevant.stories.tsx
files.Pre-merge Checklist