Repository navigation
[CLOV-64] BpkStarRating Migrate JS to TypeScript - #4523
Conversation
Convert the bpk-component-star-rating package from JavaScript/Flow to TypeScript. Replace prop-types and Flow types with typed Props exports and rename all source/test/snapshot files (.js -> .ts/.tsx). No business-logic, JSX, or rendering changes - existing snapshots remain valid. - Rename index.js -> index.ts and re-export per-component *Props types - Rename BpkStar/BpkStarRating/BpkInteractiveStar/BpkInteractiveStarRating to .tsx with typed Props exports; keep className?: string | null and destructuring defaults in place of defaultProps - Convert STAR_TYPES to as const and derive a StarType union - Migrate withInteractiveStarRatingState to .tsx with a generic class component and a public static displayName field - Rename themeAttributes/accessibility/themeAttributes-test files - Rename snapshot files to *-test.tsx.snap so Jest finds them under the new test filenames - Drop the now-unused @ts-expect-error suppressions on the index/component imports in the stories file Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
After typing the star-rating package, the @ts-expect-error suppressions on its imports in BpkRating.stories and BpkCardV2.stories became unused directives, breaking `tsc --noEmit`. Remove them. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
| ) => { | ||
| if (event) { | ||
| event.persist(); | ||
| (event as SyntheticEvent).persist(); |
There was a problem hiding this comment.
nit: event.persist() was needed in React 16 to opt out of event pooling, but React 17 removed synthetic event pooling entirely — it's been a no-op ever since.
The as SyntheticEvent cast is only here to make TS accept .persist() on a MouseEvent. Both the cast and the call can go:
onRatingSelect = (
rating: number,
event?: MouseEvent<HTMLButtonElement> | null,
) => {
const callback = () => {
if (this.props.onRatingSelect) {
this.props.onRatingSelect(rating, event);
}
};
this.setState(() => ({ rating }), callback);
};
There was a problem hiding this comment.
Done in 83a76582b — dropped both the cast and the persist call. Thanks for the catch.
| return ( | ||
| <InteractiveStarRating | ||
| {...this.props} | ||
| {...(this.props as P)} |
There was a problem hiding this comment.
Nit: worth destructuring out the HOC-owned prop before spreading, so it's clear only P props are passed down to InteractiveStarRating:
render() {
const { onRatingSelect, ...rest } = this.props;
return (
<InteractiveStarRating
{...(rest as P)}
rating={this.state.rating}
hoverRating={this.state.hoverRating}
onRatingHover={this.onRatingHover}
onMouseLeave={this.onMouseLeave}
onRatingSelect={this.onRatingSelect}
/>
);
}
as P is still needed (TS can't infer Omit<P & WithInteractiveStarRatingStateProps, 'onRatingSelect'> = P), but the intent is more explicit.
There was a problem hiding this comment.
Done in 83a76582b — destructured onRatingSelect out of this.props before spreading.
|
Hi Vincent Liu (@xiaogliu), we're moving away from snapshot tests — they tend to pass silently after an accidental For example, in BpkStar-test.tsx: // instead of // prefer |
| return ( | ||
| // $FlowFixMe[cannot-spread-inexact] - inexact rest. See decisions/flowfixme.md | ||
| <span className={[containerClassNames, halfIconClassNames]} {...rest}> | ||
| <span className={[containerClassNames, halfIconClassNames] as unknown as string} {...rest}> |
There was a problem hiding this comment.
Hi Vincent Liu (@xiaogliu) , could you improve this code as it's also a type question?
as unknown as string is hiding a real type error — React's className expects a string, not an array. The cast makes TS happy but at runtime this becomes bpk-star__container bpk-star__container--half-star,bpk-star bpk-star--half bpk-star--filled (comma-separated), which would break the styling.
There was a problem hiding this comment.
Good catch — fixed in 83a76582b by switching to .join(' '). Worth flagging that this comma-separated class string was actually a pre-existing bug from the JS code (the original snapshot had bpk-icon--rtl-support,bpk-star ... too); my as unknown as string cast was just hiding it. The half-star snapshots are updated to the corrected, space-separated form.
| | typeof ROUNDING_TYPES.down | ||
| | typeof ROUNDING_TYPES.up | ||
| | typeof ROUNDING_TYPES.nearest, | ||
| type RatingLabel = string | ((rating: number, maxRating: number) => string); |
There was a problem hiding this comment.
Small question on the typing here: Flow previously allowed the callback to return mixed, while this TS migration narrows it to string.
Given the value is used as aria-label, string seems semantically correct and probably better than preserving the overly-wide Flow type. Could we just confirm this is an intentional tightening rather than an accidental change from the migration? If yes, I think this is fine as-is.
There was a problem hiding this comment.
Yes, intentional. The return value is only ever used as the aria-label on the rendered <div role="img">, so string is the right type. The Flow mixed was wider than the actual contract.
|
|
||
| type NativeDivProps = HTMLAttributes<HTMLDivElement>; | ||
|
|
||
| export type Props = Omit<NativeDivProps, 'className' | 'aria-label'> & { |
There was a problem hiding this comment.
🤔 Question: are we intentionally expanding the public TS surface to include native div/button props here?
Flow previously described only the component-specific props, while the new TS Props extends HTMLAttributes<HTMLDivElement> / ButtonHTMLAttributes<HTMLButtonElement>. Since these Props are now exported from the package, consumers can rely on native props such as role, tabIndex, onClick, aria-*, etc. That may be fine if Backpack’s convention is to type all pass-through props this way, but it would make this a deliberate API contract rather than just an implementation detail.
Could we confirm this matches the TS migration pattern for Backpack components?
There was a problem hiding this comment.
Yes, intentional — native pass-through props (data-testid, role, aria-*, etc.) flow through ...rest onto the underlying DOM element, so the type should reflect that. Happy to discussion more as a separate convention discussion.
- BpkStar: fix half-star className being a comma-separated string at
runtime. Pre-existing bug from JS code that the TS migration's
`as unknown as string` cast was hiding. Use `.join(' ')` so the half
star gets a valid space-separated class list. Updates 4 half-star
snapshots accordingly.
- withInteractiveStarRatingState: drop the `event.persist()` call and
its `as SyntheticEvent` cast — `persist()` has been a no-op since
React 17.
- withInteractiveStarRatingState: destructure HOC-owned `onRatingSelect`
out of `this.props` before spreading, so only `P` props are forwarded
to the wrapped component.
Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
|
Agreed on the direction — explicit assertions are clearer about intent. I'd prefer to keep this PR scoped to the JS→TS rename (no behaviour change) and tackle the snapshot rewrite as a follow-up because more files need to update for BpkStarRating. I created another PR to track the 30 occurrences across the 5 test files. #4670 |
The 30 toMatchSnapshot() calls across the 5 test files in bpk-component-star-rating verified rendered HTML by full-DOM diff, which obscures intent and tends to pass silently after an accidental --updateSnapshot. Replace them with assertions that name the behaviour each test is checking: - BpkStar: assert presence/absence of star modifier classes per type and size, plus className passthrough. - BpkInteractiveStar: assert the role=button, aria-label, aria-pressed, and the inner star fill state per type/selected combo. - BpkStarRating: assert the role=img, aria-label, and the count of full/half/empty stars per rating, including maxRating and rounding behaviour. - BpkInteractiveStarRating: assert the rendered button count, fill count per rating/maxRating/hoverRating, and that onRatingHover / onRatingSelect get called with the correct star number. - withInteractiveStarRatingState: assert the initial state, that click selects (aria-pressed=true) and that hover only previews (aria-pressed stays false). Also delete the now-orphaned __snapshots__/*.snap files. No production code changes; jest now reports 0 snapshots for this package. Co-authored-by: Vincent Liu <[email protected]> Co-authored-by: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4523 to see this build running in a browser. |
Summary
bpk-component-star-ratingpackage from JavaScript/Flow to TypeScript.js->.ts/.tsx) and snapshot files (*-test.js.snap->*-test.tsx.snap) so Jest still finds them under the new test filenamespropTypes+ FlowPropswith typedPropsexports forBpkStar,BpkStarRating,BpkInteractiveStar,BpkInteractiveStarRating, re-exported fromindex.tsasBpkStarProps,BpkStarRatingProps,BpkInteractiveStarProps,BpkInteractiveStarRatingPropsSTAR_TYPEStoas constand derive aStarTypeunionwithInteractiveStarRatingStateto a generic class component (P extends object) with apublic static displayName: stringfield@ts-expect-errorsuppressions on the index/component imports in the stories fileTest plan
npm run jest -- packages/backpack-web/src/bpk-component-star-rating- 7 suites, 42 tests, 30 snapshots all passnpx tsc --noEmit- no errors inbpk-component-star-ratingnpx eslint packages/backpack-web/src/bpk-component-star-rating- no errors, no warnings🤖 Generated with Claude Code