Skip to content

Commit

Permalink
Created additional overload to prevent AdditionalProps getting inferr… (
Browse files Browse the repository at this point in the history
emotion-js#1664)

* Created additional overload to prevent AdditionalProps getting inferred as the composed component type

Fixes emotion-js#1655

* Added additional test showing additional props

* Update .changeset/lucky-swans-kneel.md

Co-Authored-By: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
JakeGinnivan and Andarist committed Dec 8, 2019
1 parent d0bae67 commit df42357
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lucky-swans-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/styled': patch
---

Fixed issue when using "component as selector" in styled interpolations which caused the wrong type to be inferred.
10 changes: 9 additions & 1 deletion packages/styled/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,18 @@ export interface CreateStyledComponent<
>
>
): StyledComponent<ComponentProps & AdditionalProps, SpecificComponentProps>

(
template: TemplateStringsArray,
...styles: Array<
Interpolation<ComponentProps & SpecificComponentProps & StyleProps>
>
): StyledComponent<ComponentProps, SpecificComponentProps>

/**
* @typeparam AdditionalProps Additional props to add to your styled component
*/
<AdditionalProps extends {} = {}>(
<AdditionalProps extends {}>(
template: TemplateStringsArray,
...styles: Array<
Interpolation<
Expand Down
53 changes: 53 additions & 0 deletions packages/styled/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,56 @@ const StyledOriginal = styled(Original, {

// No more type conflict error
;<StyledOriginal prop1="1" prop2={2} />

const Label = styled.label``
const Button = styled.button``
const Input = styled.input`
& + ${Label}: {
margin-left: 3px;
}
`

const Input2 = styled.input`
& + ${Label}: {
margin-left: 3px;
}
& + ${Button}: {
margin-left: 3px;
}
`

const Input3 = styled.input({
[`& + ${Label}`]: {
marginLeft: '3px'
}
})

interface AdditionalTest {
left: string
}
const Input4 = styled.input<AdditionalTest>`
& + ${Label}: ${props => ({
marginLeft: props.left
})}
`
;<Input
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
console.log(evt.target.value)
}
/>
;<Input2
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
console.log(evt.target.value)
}
/>
;<Input3
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
console.log(evt.target.value)
}
/>
;<Input4
left="3px"
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
console.log(evt.target.value)
}
/>

0 comments on commit df42357

Please sign in to comment.