Skip to content
This repository was archived by the owner on Aug 5, 2026. It is now read-only.

[CLOV-102] BpkPrice Migrate JS to TypeScript - #4521

Merged
Vincent Liu (xiaogliu) merged 4 commits into
mainfrom
xiaogliu/bpk-component-price-to-ts
Jun 8, 2026
Merged

Vincent Liu (xiaogliu) merged 4 commits into
mainfrom
xiaogliu/bpk-component-price-to-ts

Conversation

@xiaogliu

Copy link
Copy Markdown
Contributor

Summary

  • Convert the bpk-component-price package from JavaScript/Flow to TypeScript
  • Rename source/test files (.js → .ts/.tsx) and the snapshot file so Jest still finds it
  • Replace propTypes + Flow Props with a typed Props export, re-exported from index.ts as BpkPriceProps
  • Props extends Omit<HTMLAttributes<HTMLDivElement>, 'className'> so native div attrs flow through ...rest while keeping className?: string \| null
  • SIZES / ALIGNS use as const so consumers get literal-typed values
  • Drop the now-unused @ts-expect-error suppressions on the BpkPrice and common-types imports in the stories file
  • No business-logic, JSX, or rendering changes — existing snapshots remain valid

Notes for reviewer

  • In BpkPrice-test.tsx, the existing const icon = NewWindowIcon passes a component reference (not a JSX element). Flow accepted this; TS does not. To keep the migration pure, I cast to ReactNode (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 in bpk-component-price
  • npx eslint packages/backpack-web/src/bpk-component-price — no errors, no warnings

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]>
Copilot AI review requested due to automatic review settings May 21, 2026 04:11
@xiaogliu Vincent Liu (xiaogliu) added the minor Non breaking change label May 21, 2026
@xiaogliu Vincent Liu (xiaogliu) changed the title [BpkPrice] Migrate JS to TypeScript [CLOV-102] BpkPrice Migrate JS to TypeScript May 21, 2026
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@skyscanner-backpack-bot

skyscanner-backpack-bot Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor
Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against 9fcb0ea


import { render, screen } from '@testing-library/react';


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Line 18,23 -- blank line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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']) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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'> & {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@Faye-Xiao

Copy link
Copy Markdown
Contributor

Hi Vincent Liu (@xiaogliu) , Snapshots are discouraged — since this file is already being touched, can we convert the toMatchSnapshot() calls to explicit screen.getBy* / toHaveClass assertions?

- 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]>
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@xiaogliu

Copy link
Copy Markdown
Contributor Author

Done — replaced all toMatchSnapshot() calls with explicit screen.getByText, toHaveClass, and toBeInTheDocument assertions, and deleted the snapshot file. Each test now documents exactly what it checks.

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@Faye-Xiao

Copy link
Copy Markdown
Contributor

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?

Screenshot 2026-06-05 at 18 55 06

@xiaogliu

Copy link
Copy Markdown
Contributor Author

Hi Vincent Liu (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?

Screenshot 2026-06-05 at 18 55 06

I didn’t see there is any difference, just Percy flaky.

@Faye-Xiao

Faye (Faye-Xiao) commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

@xiaogliu
Vincent Liu (xiaogliu) merged commit 704bab7 into main Jun 8, 2026
15 checks passed
@xiaogliu
Vincent Liu (xiaogliu) deleted the xiaogliu/bpk-component-price-to-ts branch June 8, 2026 03:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

minor Non breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants