Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polymorphic-as-generic-spread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@linaria/react': patch
---

Fix TypeScript inference for generic spread props on styled components (TS2698).
9 changes: 9 additions & 0 deletions packages/react/__dtslint__/styled-jsx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ const Button = styled.button``;
<Button href="/" />;
// @ts-expect-error href is not a button property
<Button as="button" href="/" />;

declare function getProps<
T extends React.InputHTMLAttributes<HTMLInputElement>,
>(props?: T): T;

const Input = styled.input``;

// A spread of a generic call must not depend on the `as` overload.
<Input {...getProps()} disabled />;
5 changes: 5 additions & 0 deletions packages/react/src/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ type IntrinsicElementProps<TName extends keyof IntrinsicElements> =
keyof IntrinsicElements extends TName ? unknown : IntrinsicElements[TName];

type StyledComponentWithAs<TProps> = {
// Tried first so that inference in JSX does not have to go through the generic `as` overload.
(
props: TProps & { as?: undefined },
context?: any
): ReturnType<React.FunctionComponent<TProps>>;
<TAs extends keyof IntrinsicElements>(
props: TProps & { as: TAs } & NoInfer<IntrinsicElementProps<TAs>>,
context?: any
Expand Down
Loading