Repository navigation
fix(types): permissive props for un-introspectable targets, plus as-target autocomplete while typing - #5758
Conversation
🦋 Changeset detectedLatest commit: f828a2a 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.
Pull request overview
This PR adjusts the public TypeScript typings so that styled(Target) remains usable when Target’s props cannot be statically introspected (i.e. React.ComponentPropsWithRef<typeof Target> collapses to {}), restoring permissive JSX call-site behavior (including children) while keeping strict typing for normal, introspectable targets.
Changes:
- Add a type-level fallback (
WidenUntypedProps) that widens{}-like resolved prop shapes to accept arbitrary props. - Apply that fallback to
IStyledComponentso the widened behavior affects styled component call sites. - Add a type test that mirrors Mantine v7’s polymorphic-factory callable+statics shape, plus a changeset entry.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/styled-components/src/types.ts | Introduces and applies a conditional “widen props when keyof Props is never” fallback for styled components. |
| packages/styled-components/src/test/types.tsx | Adds a regression test modeling Mantine’s polymorphic-factory type shape and expected permissive JSX behavior. |
| .changeset/fix-untyped-target-props.md | Documents the patch-level TypeScript typing fix in the release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…le targets
Wrapping a component whose props can't be statically read -- most notably
Mantine v7's polymorphic-factory components (Button, Card, Menu.Item),
whose generic callable signature defeats React.ComponentPropsWithRef and
collapses to {} -- made the resulting styled component reject every prop,
including children, at the JSX call site and via .attrs().
Detect the un-introspectable case and widen to a permissive prop bag for it
alone; targets with readable prop types keep full checking. The widening is
confined to the JSX call surface, never the statics or defaultProps, so the
real Props can't leak past the call site. The "no known keys" test is
distributed over the prop type first so a bare union of disjoint shapes
(keyof intersects to never despite being introspectable) isn't widened by
mistake.
Closes #5756
…get completion survives mid-typing A partially-typed or unknown attribute on a polymorphic styled component (e.g. <Comp as="video" foo l|>) matched none of the call overloads, so overload resolution failed and JSX attribute completion dropped to nothing -- the as-target's props (video's loop/controls/...) disappeared while typing. Replacing the overload set with a single conditional call signature keeps completion resolved at all times. The branching (PolymorphicCallProps) preserves prior behavior: a real as-target substitutes its props; no-as / wrapped-as (Url-typed) keeps Substitute-free base props for ref inference on spread (#5687), wrapped-as assignability (#5734), and plain completion (#5741); forwardedAs substitutes the forwarded target. The single signature instantiates a conditional per JSX usage where the overload set resolved plain usage to a free non-generic overload, so some extra type work is inherent. Two changes keep it small: the target test uses string | AnyComponent rather than the literal-heavy WebTarget (same set, ~6% fewer instantiations across call sites), and the no-target result is a shared alias so it instantiates once. Net vs the prior overloads: instantiations 1,608,782 -> 1,696,358 (+5.4%), check time 2.94s -> 3.22s.
This PR lands two related TypeScript fixes for polymorphic styled components.
1. Permissive props for un-introspectable targets (#5756)
Wrapping a component whose props can't be statically read produced a styled component that rejected every prop at the JSX call site, including
children. This hit everystyled(X)whereXis a Mantine v7 polymorphic-factory component (Button,Card,Menu.Item,Badge,SegmentedControl, and friends), whose generic callable signature makesReact.ComponentPropsWithRef<typeof X>resolve to{}.When a target's props resolve to an empty
{}, the styled component now falls back to accepting arbitrary props instead of rejecting all of them, both at the JSX call site and via.attrs(). Components whose prop types can be read are unaffected and stay fully type-checked.Details
{ a: string } | { b: string }, whosekeyofisnever) stays strict rather than going permissive. All-optional shapes ({ a?: string }) also stay strict.unknown-typed, notany, so reading props back off the component (React.ComponentProps<typeof Styled>) doesn't disable type-checking in consumer code.defaultPropskeep their real prop types internally.Known limitation
For an un-introspectable target there is no base type to substitute against, so
asis accepted but theas-target's own props aren't individually type-checked (e.g.hrefonas="a"isn't anchor-validated). An introspectable target keeps fullastyping. A genuinely propless component (() => <div/>) is indistinguishable from an un-introspectable one and also becomes permissive (both resolve to{}).2.
as-target autocomplete survives mid-typingOn a polymorphic styled component rendering a different element through
as(for exampleas="video"), starting to type a new prop name could make the whole attribute suggestion list vanish, so the rendered element's props (loop,controls, and the rest) disappeared while you were typing them. They now keep autocompleting as you go.Details
The previous shape exposed several call overloads. A half-typed or unknown attribute matched none of them, overload resolution failed, and JSX completion dropped to nothing. A single conditional call signature always resolves, so completion stays live. The conditional preserves the prior behaviors: a real
as-target substitutes its props; noas, or anastyped as the wrapped component's own non-target type (e.g. Next.jsLink'sas?: Url), keeps base props withasoptional and assignable;forwardedAssubstitutes the forwarded target.A single generic signature does a little more type work per call site than the overload set did. The cost is kept small (target test against
string | AnyComponentrather than the literal-heavyWebTarget, and a shared alias for the no-target result); net is roughly +5% type instantiations across the type test suite, with check time effectively unchanged.Verification
Verified against real
@mantine/core@7:React.ComponentPropsWithRef<typeof Button>resolves to{}, the wrapped styled component now accepts arbitrary props andchildren(at the call site and through.attrs()), and normally typed styled components still reject unknown or mistyped props. Autocomplete was confirmed via the editor language service for both the mid-typingas-target case and plain usage. Type tests covering the Mantine factory shape, the union edge cases, and the existingas/forwardedAsbehaviors are included; the full runtime suite (924 tests) passes.Closes #5756