Skip to content

Commit

Permalink
Removed type for as prop for composite components (#2240)
Browse files Browse the repository at this point in the history
* fix #2238: TS declarations - remove `as` prop from components composed with `styled`

* Add changeset to PR 2240

Co-authored-by: Mateusz Wolszczak <mateusz@wolszczak.io>
  • Loading branch information
Mateusz Wolszczak and Mateusz Wolszczak committed Feb 6, 2021
1 parent f3c2e81 commit d029350
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-chairs-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/styled': patch
---

`as` prop has been removed from TypeScript declarations for composite components. This prop has not actually been handled by default by `styled` for those components - to make `styled` handle it you need to provide a custom `shouldForwardProp` that doesn't forward the `as` prop.
4 changes: 0 additions & 4 deletions packages/styled/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export interface CreateStyled {
): CreateStyledComponent<
Pick<PropsOf<C>, ForwardedProps> & {
theme?: Theme
as?: React.ElementType
},
{},
{
Expand All @@ -142,7 +141,6 @@ export interface CreateStyled {
): CreateStyledComponent<
PropsOf<C> & {
theme?: Theme
as?: React.ElementType
},
{},
{
Expand All @@ -161,7 +159,6 @@ export interface CreateStyled {
): CreateStyledComponent<
Pick<PropsOf<C>, ForwardedProps> & {
theme?: Theme
as?: React.ElementType
}
>

Expand All @@ -171,7 +168,6 @@ export interface CreateStyled {
): CreateStyledComponent<
PropsOf<C> & {
theme?: Theme
as?: React.ElementType
}
>

Expand Down
35 changes: 35 additions & 0 deletions packages/styled/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,39 @@ const Input5 = styled.input`
`
;<StyledWithAs as="section" />
;<StyledWithAs as={Section} />
// $ExpectError
;<StyledWithAs as="random string" />

const ComposedWithAs = styled(StyledWithAs)`
flex-direction: column;
`
;<ComposedWithAs as="section" />
;<ComposedWithAs as={Section} />
// $ExpectError
;<ComposedWithAs as="random string" />

const ComponentWithAs: React.FC<{ as: string; className?: string }> = ({
as,
className
}) => <div className={className}>{as}</div>

const StyledComp = styled(ComponentWithAs)`
background: orange;
`
;<StyledComp as="random string" />
// $ExpectError
;<StyledComp as={Section} />

const ComponentWithoutAs: React.FC<{ className?: string }> = props => (
<div {...props} />
)
const StyledCompWithoutAs = styled(ComponentWithoutAs)`
background: hotpink;
`
// $ExpectError
;<StyledCompWithoutAs as="random string" />
// $ExpectError
;<StyledCompWithoutAs as="section" />
// $ExpectError
;<StyledCompWithoutAs as={Section} />
}

0 comments on commit d029350

Please sign in to comment.