Repository navigation
[CLOV-102] BpkPrice Migrate JS to TypeScript - #4521
Conversation
Convert the bpk-component-price package from JavaScript/Flow to TypeScript. Replace prop-types and Flow Props 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 BpkPriceProps type - Rename BpkPrice.js -> BpkPrice.tsx; type Props extends Omit<HTMLAttributes<HTMLDivElement>, 'className'> so native div props flow through ...rest while keeping className?: string | null - Rename common-types.js -> common-types.ts; use `as const` so SIZES / ALIGNS export literal-typed values - Rename test/accessibility-test/snapshot files; type the BpkPrice-test props variable as BpkPriceProps and cast the icon NewWindowIcon reference to ReactNode to preserve the existing (already-empty) icon span in snapshots - Drop the now-unused @ts-expect-error suppressions on the BpkPrice and common-types imports in the stories file Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4521 to see this build running in a browser. |
The BpkPrice TS migration exposed two latent issues in card stories: - Unused @ts-expect-error suppressions on BpkPrice imports (now typed) - BpkCardV2 stories used SIZES.Large (capitalised), which compiled only because the SIZES import was untyped. With SIZES now `as const` the correct lowercase `large` key is enforced. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4521 to see this build running in a browser. |
|
|
||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
|
|
There was a problem hiding this comment.
nit: Line 18,23 -- blank line
There was a problem hiding this comment.
Done — removed the extra blank lines on lines 18 and 23.
| const getClassName = cssModules(STYLES); | ||
|
|
||
| const getPriceTextStyle = (size: $Values<typeof SIZES>) => { | ||
| const getPriceTextStyle = (size: Props['size']) => { |
There was a problem hiding this comment.
Nit: Props['size'] resolves to 'xsmall' | 'small' | 'medium' | 'large' | undefined,
but neither function handles undefined — passing it silently falls through to the
default return value. Since size already has a default param in the component,
undefined will never actually reach here, but the signature is wider than needed.
Consider tightening to NonNullable<Props['size']> (or the raw union directly) so
TS catches any future callsite that passes undefined at compile time rather than
silently returning heading5 / xs at runtime.
There was a problem hiding this comment.
Done — tightened both helper signatures to NonNullable<Props['size']>. TS will now catch any future callsite that passes undefined at compile time instead of silently falling through to the default return value.
| leadingText: ?string, | ||
| type NativeDivProps = HTMLAttributes<HTMLDivElement>; | ||
|
|
||
| export type Props = Omit<NativeDivProps, 'className'> & { |
There was a problem hiding this comment.
Question: are we intentionally expanding the public API surface here?
Previously the Flow type only described BpkPrice-specific props, while this now inherits all native HTMLAttributes<HTMLDivElement> props. Since BpkPriceProps is exported from the package, consumers can now rely on native props like role, tabIndex, onClick, aria-*, etc.
That may be fine if this matches Backpack’s TS migration pattern, but it would make those pass-through props part of the public contract rather than just an implementation detail. Could we confirm that’s intentional?
There was a problem hiding this comment.
Yes, intentional — this matches the established Backpack TS migration pattern. BpkPagination and BpkCheckbox both extend native HTML attribute types via Omit<HTMLAttributes<...>, 'className'>. The runtime behaviour is unchanged (the component already {...rest}-spread onto the root <div> under Flow); the type just makes that contract explicit so callers can pass role, tabIndex, onClick, aria-*, etc. without a @ts-expect-error.
| const trailingText = 'per day'; | ||
| const icon = NewWindowIcon; | ||
| let props; | ||
| const icon = NewWindowIcon as unknown as ReactNode; |
There was a problem hiding this comment.
nit: Could we avoid casting the icon component to ReactNode here?
BpkPrice renders icon as a React child, so the prop should ideally receive an element, e.g. <NewWindowIcon />, rather than the component function itself. The as unknown as ReactNode cast makes the test pass type-checking but could hide an invalid usage pattern.
There was a problem hiding this comment.
Done — replaced the NewWindowIcon as unknown as ReactNode cast with <NewWindowIcon /> rendered inline. The test now passes an actual React element, which matches the intended usage of the icon prop.
|
Hi Vincent Liu (@xiaogliu) , Snapshots are discouraged — since this file is already being touched, can we convert the |
- Replace toMatchSnapshot() assertions with explicit getBy* / toHaveClass assertions and delete the snapshot file (review feedback: snapshots discouraged; better to test behaviour than the rendered HTML) - Pass <NewWindowIcon /> instead of casting the component reference, removing the as-unknown-as-ReactNode cast and exercising the icon rendering path properly - Tighten getPriceTextStyle / getDefaultTextStyle params from Props['size'] to NonNullable<Props['size']> so future callsites passing undefined are caught at compile time - Drop the stray blank lines in BpkPrice-test.tsx imports Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Visit https://backpack.github.io/storybook-prs/4521 to see this build running in a browser. |
|
Done — replaced all |
|
Visit https://backpack.github.io/storybook-prs/4521 to see this build running in a browser. |
|
Hi Vincent Liu (@xiaogliu), I checked the Percy build and found a slight change on the bpk-price-ranger visual test, as shown in the screenshot below. Could you double-check if it's a flaky test or a real dom change, as price ranger uses the BpkPrice?
|
I didn’t see there is any difference, just Percy flaky. |
|
Approved the precy failure https://percy.io/09e69e44/web/backpack/builds/50523019?utm_campaign=09e69e44&utm_content=backpack&utm_source=github_status_public as the failure came from flaky tests. |


Summary
bpk-component-pricepackage from JavaScript/Flow to TypeScript.js→.ts/.tsx) and the snapshot file so Jest still finds itpropTypes+ FlowPropswith a typedPropsexport, re-exported fromindex.tsasBpkPricePropsPropsextendsOmit<HTMLAttributes<HTMLDivElement>, 'className'>so native div attrs flow through...restwhile keepingclassName?: string \| nullSIZES/ALIGNSuseas constso consumers get literal-typed values@ts-expect-errorsuppressions on theBpkPriceandcommon-typesimports in the stories fileNotes for reviewer
BpkPrice-test.tsx, the existingconst icon = NewWindowIconpasses a component reference (not a JSX element). Flow accepted this; TS does not. To keep the migration pure, I cast toReactNode(as unknown as ReactNode) so the existing snapshots — which already render an empty<span class="bpk-price__icon ...">because React silently does nothing with a function child — continue to match. Worth a follow-up to either drop the icon test cases or pass<NewWindowIcon />and refresh the snapshots.Test plan
npm run jest -- packages/backpack-web/src/bpk-component-price(4 suites, 92 tests, 64 snapshots all pass)npx tsc --noEmit— no errors inbpk-component-pricenpx eslint packages/backpack-web/src/bpk-component-price— no errors, no warnings