Repository navigation
fix(types): cheaper annotated declaration sites, and sound generic polymorphic props - #5794
Conversation
…olymorphic props Two independent type fixes, each with its own changeset: - Rebase the widened inline `style` prop on `React.CSSProperties` so an explicit `IStyledComponentBase`/`IStyledComponent` annotation, which isolatedDeclarations and `.d.ts`-emitting packages must write, relates against a shared csstype instantiation instead of walking csstype per component. About 21% fewer types created at annotated declaration sites, with no behavior or call-site change. - Gate the style widening off for a prop bag carrying a string index signature, so `styled()` wrapping a generic polymorphic component keeps its declared props narrow instead of collapsing them to `any` under built-in `Omit` (#5756). Also adds an `annotated` kind to the consumer type-perf fixture so the declaration-site annotation axis is guarded going forward.
🦋 Changeset detectedLatest commit: 1cbf893 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Important
Both fixes verify clean — I rebuilt dist, ran the #5756 generic-polymorphic repro (kkwasny-rtbh's exact PolymorphicComponentProps shape) and the repo's GenericPolyButton fixture against the built declarations on TS 5.9.3 / @types/react 18, and confirmed the new @ts-expect-errors are genuine positive controls (removing the gate turns them into unused directives). One consequence of the index gate is real but undersold in what this PR ships: it also disables style custom-property widening for non-any index-signed prop bags, not just [k: string]: any ones. The direction is defensible (the wrapper's style now types exactly like the unwrapped component, and the key-preserving alternative measured expensive), but the "only ... a style that is already any" claim is inaccurate for concrete-typed index bags, and nothing pins the new style behavior on them. See the inline comment on the gate.
Reviewed changes
- Rebased the widened inline
styleontoReact.CSSProperties—CSSPropertiesWithVarsintypes.tsnow builds on React's ownCSSProperties, so a hand-written declaration annotation (IStyledComponentBase<'web', … & { style?: React.CSSProperties }>) relates to the styled result'sstyleby the intersection-member fast path instead of walking csstype member by member. No change to acceptedstylevalues (custom props,style={undefined}, bad values all behave as before). - Gated
stylewidening off for string-index-signed prop bags — the newstring extends keyof Pbranch inOverrideStylereturns such bags untouched, keeping a generic polymorphic component's narrow props intact after introspection and makingstyled(Button)reject prop values the component itself rejects (#5756). - Added an
annotatedkind to the type-perf fixture — guards the declaration-site axis (weights moved; budget re-measured intype-perf.budget.jsonon TS 5.9.3 /@types/react18, instantiations 850,743 → 936,809), withdocs/type-performance.mdgaining a "declaration-site relation" section and a 6th load-bearing bullet. - Replaced the #5756 BASELINE-PIN in
src/test/types.tsx— the pinned "wrapper accepts a prop the direct call rejects" is now a positive control asserting both the direct and wrapped calls reject a badvariant, with a valid call added to prove the new behavior doesn't over-prune.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
…e-perf fixture
- Skip re-resolving the render target's props when object-form `.attrs()` leaves
the target unchanged (the common case). That path rebuilt the ~265-key
intrinsic prop bag through `ComponentPropsWithRef` on every `.attrs` call, only
to reproduce what `OuterProps` already held; the fast path returns it directly.
About 6.8% fewer consumer type-check instantiations, with no change to the
resulting component's accepted props. Redirecting the target (`.attrs({ as })`,
including the function form) is unaffected.
- Add four exotic fixture kinds, each pricing a `types.ts` conditional arm no
existing kind reached and each doubling as a compile-fail canary:
`permissiveFactory` (the un-introspectable `WidenUntypedProps` fallback),
`genericPoly` (the #5756 `string extends keyof P` `OverrideStyle` gate),
`attrsAsRedirect` (function-form `.attrs` union-target seam), and
`disjointTarget` (the `keyof B extends never` empty-bag guard). Plus `style` and
`ref` call-site variants on `plainTag`, and `canary.tsx` pins for the two #5756
soundness properties. The budget was re-measured; the growth is the fixture, not
the cost.
There was a problem hiding this comment.
ℹ️ No new issues in this increment — the delta is clean. This review is not an approval solely because the previously-flagged typed-index
stylecontract thread (on theOverrideStylegate) remains open and untouched by these commits.
Reviewed changes
Incremental since the prior pullfrog review (9031285), per commit 7d2696ec:
- Added an
.attrstarget-unchanged fast path — object-form.attrs()leaves the render target as-is, so[PrivateResolvedTarget] extends [Target]returnsMergeProps<OuterProps, Props>directly instead of re-merging the ~265-keyComponentPropsWithRef<Target>bag. The bracket keeps a function-form.attrs(resolved target is a union) on the re-merge branch, preserving the TS2589-sensitive seam and the.attrs({ as })redirect path. I traced the type algebra across the common shapes (fresh tags, wrapped components, chained.attrs, plain-function targets,KnownTarget-typed function-form redirects); the skip is behavior-preserving and only ever avoids re-reproducing whatOuterPropsalready carried. - Broadened the type-perf fixture with four exotic kinds —
permissiveFactory(Mantine-style un-introspectable target),genericPoly(the #5756 gate),attrsAsRedirect(function-form.attrsunion-target seam),disjointTarget(thekeyof B extends neverguard) — plusstyle/refcall-site variants onplainTag. Each kind prices a conditional arm no other kind reaches; each doubles as a compile-fail canary. ThegenericPoly/permissiveFactorykinds correctly cannot catch a gate regression on their own (a valid value still compiles when the gate is dropped), which is why the canary pins (CanaryPolyBad,CanaryFactoryOk) exist and are genuine positive controls — a gate regression turnsCanaryPolyBad's@ts-expect-errorinto an unused directive (TS2578) and fails the clean-compile gate. - Re-measured the type-perf budget and updated
docs/type-performance.mdwith the fast-path rationale and the four-kind canary taxonomy. The measured drift (instantiations 936,809 → 926,001; types 34,797 → 42,528; memory +5%) is fixture growth from the exotic kinds on top of the fast-path savings, consistent with how the fixture treatsunionTarget/annotated. - Added the consumer-facing changeset describing the
.attrstype-check speedup; correct wording, no internal names.
No new findings in this delta.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
The largest single cost of `.attrs` on a tag is `MakeAttrsOptional`'s mapped pass over the ~266-key widened intrinsic bag (~30% of that kind's types), not the target-redirect seam: a union redirect and a single-concrete redirect price identically, and collapsing the `ComponentPropsWithRef` re-merge moves ~6 types while breaking four redirect contracts. Switch that pass from `FastOmit` to built-in `Omit` distributed through an outer `P extends unknown`. Built-in `Omit` is the more optimized here (the same result the `OverrideStyle` note records), and the distribution runs it per union member so a union-props attrs target (`styled(Pressable).attrs(...)`) keeps its member-specific keys instead of collapsing to the union's common ones; a bare undistributed `Omit` drops `href`. Consumer fixture: -7.5% types, -4.1% instantiations, memory flat. Contract suite (web + native) and every `.attrs` form (object, function, as-redirect, chained, union-target) unchanged.

Type-only work on the 6.x type surface: two consumer-facing fixes,
.attrstype-check speedups, and broadened perf coverage. No runtime change; the consumer type-perf budget and the web + native type-test suites stay green.1. Explicitly annotated styled components type-check faster
Assigning a styled component to an explicit type, as
isolatedDeclarationsand any package that emits.d.tsfiles must (const Button: IStyledComponentBase<'web', ...> = styled.button\`), used to cost several times more to check than an inferred one: the annotation'sstyleand the component's widenedstylewere two different csstype representations compared property by property. The inlinestylewidening now builds on React's ownCSSProperties, the same type a hand-written annotation carries, so the comparison short-circuits (~21% fewer types created for that pattern on a 40-component fixture), with no change to whatstyleaccepts. Adds a declaration-site (annotated`) kind to the type-perf fixture so the axis is guarded.2.
styled()on a generic polymorphic component keeps its props narrow (#5756)Wrapping a component whose props are generic over an element type (
<C extends React.ElementType>(props: PolymorphicProps<C, OwnProps>)) used to let the styled result accept prop values the component itself rejects:styled(Button)would takevariant="anything"even though<Button variant="anything">is a type error. Introspecting such a component resolves props at theReact.ElementTypeconstraint, where the bag picks up a[key: string]: anyindex signature; the style widening'sOmitthen collapsed the declared narrow props into that index. The widening now leaves an index-signature bag untouched, so the narrow props survive and a bad value is caught in the wrapper too. Valid props, children, and plain targets are unaffected.3.
.attrs()is cheaper to type-checkTwo costs on the
.attrspath are removed. Object-form.attrs()leaves the rendered target unchanged but still re-resolved that target's whole prop bag on every call, so.attrson an HTML/SVG tag cost far more than on a wrapped component; it now reuses the props already resolved for the tag. And making attrs-provided keys optional ran an avoidably expensive mapped pass over the target's full prop set; switching that to a distributed built-inOmitcuts it further while keeping a union-props attrs target's member-specific props intact. Net on the consumer fixture: about 4% fewer instantiations and 7.5% fewer types created versus the start of this PR, with every.attrsform (object, function,as-redirect, chained) behaving identically.Broadened perf coverage (no runtime effect)
The type-perf fixture gained four exotic kinds, each pricing a distinct type-system code path and each doubling as a compile-fail canary: an un-introspectable polymorphic-factory target, a generic polymorphic target (guards fix #2), a function-form
.attrstarget redirect, and a no-shared-keys union target. Plusstyle/refcall-site variants and canary pins for the #5756 soundness properties. Each is a changeset-free fixture change; the budget was re-measured.