Skip to content

Better props validation and standardize component template#5476

Merged
compulim merged 53 commits intomicrosoft:mainfrom
compulim:feat-better-types
May 12, 2025
Merged

Better props validation and standardize component template#5476
compulim merged 53 commits intomicrosoft:mainfrom
compulim:feat-better-types

Conversation

@compulim
Copy link
Copy Markdown
Contributor

@compulim compulim commented May 11, 2025

Changelog Entry

Changed

Description

Modernize components by dropping FC<...>, VFC<...>, defaultProps and propTypes, and memoizing components.

Design

  • Use valibot to validate props on development build
    • Validation is skipped on production build
  • Remove FC<...>, VFC<...>, defaultProps and propTypes
  • Export memoized component, prop schema and its input type
  • Bump valibot@1.1.0

Template

const myComponentPropsSchema = pipe(
  object({
    someValue: string()
  }),
  readonly()
);

type MyComponentProps = InferInput<typeof myComponentPropsSchema>;

function MyComponent(props: MyComponentProps) {
  const { someValue } = validateProps(myComponentPropsSchema, props);

  // ...
}

export default memo(MyComponent);
export { myComponentPropsSchema, type MyComponentProps };

Validating props

Props are validated only in development mode (until we have a design for production mode.)

There are 2 types of validation:

  • Strict isolation: props are validated, and the validated result is a clone to provide extra isolation and support transformation
    • This works best when props need to be transformed
  • Validating only: props are validated but not cloned/transformed
    • This works best when props contains array or object which are prone to change on clone

Specific Changes

(See design section)

  • I have added tests and executed them locally
  • I have updated CHANGELOG.md
  • I have updated documentation

Review Checklist

This section is for contributors to review your work.

  • Accessibility reviewed (tab order, content readability, alt text, color contrast)
  • Browser and platform compatibilities reviewed
  • CSS styles reviewed (minimal rules, no z-index)
  • Documents reviewed (docs, samples, live demo)
  • Internationalization reviewed (strings, unit formatting)
  • package.json and package-lock.json reviewed
  • Security reviewed (no data URIs, check for nonce leak)
  • Tests reviewed (coverage, legitimacy)

@compulim compulim changed the title [WIP] Better props validation and standardize component template Better props validation and standardize component template May 12, 2025
@compulim compulim marked this pull request as ready for review May 12, 2025 08:12
Copy link
Copy Markdown
Collaborator

@OEvgeny OEvgeny left a comment

Choose a reason for hiding this comment

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

I hope it won't affect our perf 🤣

@compulim
Copy link
Copy Markdown
Contributor Author

I looked at rendering.profiling.html and the graph didn't looks changed for me. 🙂

@compulim compulim merged commit 2986fa9 into microsoft:main May 12, 2025
25 checks passed
@compulim compulim deleted the feat-better-types branch May 12, 2025 17:47
This was referenced Dec 19, 2025
@compulim compulim mentioned this pull request Mar 5, 2026
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants