Skip to content

fix(types): permissive props for un-introspectable targets, plus as-target autocomplete while typing - #5758

Merged
quantizor merged 2 commits into
6.xfrom
fix/issue-5756
Jun 2, 2026
Merged

quantizor merged 2 commits into
6.xfrom
fix/issue-5756

Conversation

@quantizor

@quantizor quantizor commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

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 every styled(X) where X is a Mantine v7 polymorphic-factory component (Button, Card, Menu.Item, Badge, SegmentedControl, and friends), whose generic callable signature makes React.ComponentPropsWithRef<typeof X> resolve to {}.

import styled from 'styled-components';
import { Button } from '@mantine/core';

const Styled = styled(Button)`color: red;`;

// before: TS2769, `variant` and `children` rejected
// after: accepted
const usage = <Styled variant="filled">hi</Styled>;

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

  • The detection keys on the props having no known keys. It's distributed over unions, so a bare union of disjoint shapes ({ a: string } | { b: string }, whose keyof is never) stays strict rather than going permissive. All-optional shapes ({ a?: string }) also stay strict.
  • The permissive fallback is unknown-typed, not any, so reading props back off the component (React.ComponentProps<typeof Styled>) doesn't disable type-checking in consumer code.
  • The widening is confined to the call surface and the attrs input; component statics and defaultProps keep their real prop types internally.

Known limitation

For an un-introspectable target there is no base type to substitute against, so as is accepted but the as-target's own props aren't individually type-checked (e.g. href on as="a" isn't anchor-validated). An introspectable target keeps full as typing. 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-typing

On a polymorphic styled component rendering a different element through as (for example as="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; no as, or an as typed as the wrapped component's own non-target type (e.g. Next.js Link's as?: Url), keeps base props with as optional and assignable; forwardedAs substitutes 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 | AnyComponent rather than the literal-heavy WebTarget, 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 and children (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-typing as-target case and plain usage. Type tests covering the Mantine factory shape, the union edge cases, and the existing as / forwardedAs behaviors are included; the full runtime suite (924 tests) passes.

Closes #5756

Copilot AI review requested due to automatic review settings June 1, 2026 21:47
@changeset-bot

changeset-bot Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f828a2a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 IStyledComponent so 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.

Comment thread packages/styled-components/src/types.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread packages/styled-components/src/types.ts Resolved
quantizor added 2 commits June 1, 2026 20:29
…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.
@quantizor
quantizor requested a review from Copilot June 2, 2026 02:06
@quantizor quantizor changed the title fix(types): keep styled() permissive for un-introspectable targets fix(types): permissive props for un-introspectable targets, plus as-target autocomplete while typing Jun 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@quantizor
quantizor merged commit f692ec2 into 6.x Jun 2, 2026
8 checks passed
@quantizor
quantizor deleted the fix/issue-5756 branch June 2, 2026 02:14
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