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

Box Control: Add Runtime Check & Conditional Types for presets and presetKey Props #68385

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

im3dabasia
Copy link
Contributor

@im3dabasia im3dabasia commented Dec 30, 2024

Follow up to this Comment: #67688 (comment)

What?

  • Added a runtime warning when only one of presets or presetKey props is defined.
  • Introduced conditional types to ensure presetKey is required if presets is specified.

Why?

  • Prevents potential misconfigurations by ensuring that both props are used correctly together.
  • Enhances type safety and developer experience by enforcing clear prop relationships.

How?

  • Implemented a runtime check for the presets and presetKey props to trigger a warning if only one is defined.
  • Updated TypeScript types to make presetKey non-optional when presets is provided.

Testing Instructions

  1. Define only one of the presets or presetKey props in a component. (For my testing purposes I used the BoxControl storybook)
  2. Ensure a runtime warning is displayed.
  3. Verify that presetKey is required when presets is defined.

Screencast

Screen.Recording.2025-01-03.at.1.18.21.PM.mov

@im3dabasia im3dabasia marked this pull request as ready for review January 3, 2025 07:53
@im3dabasia im3dabasia requested a review from ajitbohra as a code owner January 3, 2025 07:53
Copy link

github-actions bot commented Jan 3, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: im3dabasia <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@im3dabasia
Copy link
Contributor Author

Hi @mirka
When you have a moment, Please review my PR.

Thank you 🙇

@mirka mirka requested a review from ciampo January 13, 2025 10:48
@Mamaduka Mamaduka added [Type] Code Quality Issues or PRs that relate to code quality [Package] Components /packages/components labels Jan 13, 2025
packages/components/src/box-control/index.tsx Show resolved Hide resolved
Comment on lines +112 to +114
presetKey?: BoxControlProps[ 'presets' ] extends undefined
? never
: string;
Copy link
Contributor

@ciampo ciampo Jan 16, 2025

Choose a reason for hiding this comment

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

This is not the correct way to define conditional types. You can test by yourself, by trying to define BoxControl with only one prop between presets and presetKey — if the types were defined correctly, TS should error. But it doesn't.

We have other examples of such conditional types in the codebase (example). I also put together a quick TS playground to show how the proposed approach doesn't work, and how a correct approach would work. Hope it helps!

If not, happy to provide the code to implement the conditional types in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants