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-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linaria/react": patch
---

Add typing for intrinsic props selected through the polymorphic `as` prop on styled components.
9 changes: 9 additions & 0 deletions packages/react/__dtslint__/styled-jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';

import { styled } from '..';

const Button = styled.button``;

<Button as="a" href="/" />;
// @ts-expect-error href requires an anchor-like target
<Button href="/" />;
6 changes: 6 additions & 0 deletions packages/react/__dtslint__/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const StyledDiv = styled.div``;
// $ExpectType "extends"
isExtends<typeof StyledDiv, React.FC<React.DetailedHTMLProps<any, any>>>();

const StyledButton = styled.button``;
StyledButton({ as: 'a', href: '/' });
StyledButton.defaultProps = { as: 'a' };
// @ts-expect-error href requires an anchor-like target
StyledButton({ href: '/' });

const A = (): React.ReactElement => React.createElement('div', null);
// @ts-expect-error
styled(A)``;
Expand Down
1 change: 1 addition & 0 deletions packages/react/__dtslint__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"jsx": "react",
"lib": ["es6"],
"target": "ES2015",
"esModuleInterop": true,
Expand Down
32 changes: 29 additions & 3 deletions packages/react/src/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,36 @@ function styled(tag: any): any {
};
}

type FunctionComponentStatic<
TProps,
TKey extends PropertyKey,
> = TKey extends keyof React.FunctionComponent<TProps>
? React.FunctionComponent<TProps>[TKey]
: never;

type StyledComponentWithAs<TProps> = {
<TAs extends keyof IntrinsicElements>(
props: TProps & { as: TAs } & IntrinsicElements[TAs],
context?: any
): ReturnType<React.FunctionComponent>;
(
props: TProps & { as?: React.ElementType },
context?: any
): ReturnType<React.FunctionComponent<TProps>>;
contextTypes?: FunctionComponentStatic<TProps, 'contextTypes'>;
defaultProps?: FunctionComponentStatic<
TProps & { as?: React.ElementType },
'defaultProps'
>;
displayName?: FunctionComponentStatic<TProps, 'displayName'>;
propTypes?: FunctionComponentStatic<
TProps & { as?: React.ElementType },
'propTypes'
>;
};

export type StyledComponent<T> = WYWEvalMeta &
([T] extends [React.FunctionComponent<any>]
? T
: React.FunctionComponent<T & { as?: React.ElementType }>);
([T] extends [React.FunctionComponent<any>] ? T : StyledComponentWithAs<T>);

type StaticPlaceholder = string | number | CSSProperties | WYWEvalMeta;

Expand Down
Loading