Components: Update polymorphism types to allow all attributes when specifying as#80705
Components: Update polymorphism types to allow all attributes when specifying as#80705aduth wants to merge 4 commits into
as#80705Conversation
|
Size Change: -7 B (0%) Total Size: 7.75 MB 📦 View Changed
|
| /** | ||
| * The content to render inside the wrapper. | ||
| */ | ||
| children?: ReactNode; |
There was a problem hiding this comment.
From what I understand, this should have always been here, and the previous polymorphism types incorrectly typed children for non-polymorphic components. Generally it should be expected that components define their own children types, which we're more consistent about in @wordpress/ui (e.g. ComponentProps explicitly omits children)
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Oof, type-checking failures in CI. Guessing some local cache might have misled me locally. I'll throw this back to draft temporarily and revisit it on Monday, unless someone wants to go ahead and carry the torch on this one. Edit: Nevermind, decided to tackle it now. |
| path={ `/${ tab.name }` } | ||
| // @ts-expect-error: Navigator.Button is currently typed in a way that prevents Item from being passed in | ||
| as={ Item } | ||
| isAction |
There was a problem hiding this comment.
This is safe:
gutenberg/packages/components/CHANGELOG.md
Line 3309 in c6c8e71
Navigator.Button provides onClick:
|
Flaky tests detected in 7e5d36a. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/30124455510
|
What?
Updates the polymorphism types used in
@wordpress/componentsto expose all possible HTML attributes whenasis specified, rather than narrowing the types based on the tagName provided toas.The performance impact on TypeScript builds is significant, reducing memory usage by -47% (7.4GB to 4.0GB), TypeScript build time by -49% (13.7s to 7.0s), and internal instantiations by -54% (42.5M to 19.5M).
Why?
How?
As described in #80655 , these polymorphism types are a major contributor to overall TypeScript build times and memory consumption. While #80655 tried to fully maintain the original intent of the polymorphism types, the changes here propose a more drastic change: If
asis provided, all valid HTML attributes are accessible on that component. This means someone could do something like<VisuallyHidden as="div" htmlFor="id" />and TypeScript would consider it valid, which was not the case before now, ashtmlForis not a valid attribute fordivelements.This is meant to be a reasonable compromise given the circumstances, and how new development in
@wordpress/uileans onrenderprops instead ofaspolymorphism.This isn't all bad though, since:
asis provided, and otherwise the component's own props are the only ones available.htmlForis astring, for example.Testing Instructions
Verify type-checking passes:
For bonus points, try some of the scenarios described under "How", like how you can't assign
htmlForunless specifyingas, or thathtmlForcan be used withas="div", or howhtmlForcan't be assigned to a type other than string.Also check performance for yourself using snippets from #80364 "Testing Instructions":
Use of AI Tools
Used Cursor IDE + Auto (likely Composer) model to research and implement, with manual iterations, particularly on the code comments.