Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve types for .attrs() #4288

Merged
merged 6 commits into from Apr 30, 2024
Merged

Conversation

uhyo
Copy link
Contributor

@uhyo uhyo commented Apr 7, 2024

closes #4076
closes #4138
closes #4183

Hello, the above issue is blocking us from upgrading to v6, so here is a possible fix.

Improvement

The improvement brought by this PR can be seen in this example:

  const DivWithRequiredFooBar = styled.div<{ foo: number; bar: string }>``;

  // foo is provided, so it becomes optional
  const DivWithRequiredBar = styled(DivWithRequiredFooBar).attrs({ foo: 42 })`
    margin; ${props => props.foo * 10}px;
  `;
  // @ts-expect-error must provide bar
  <DivWithRequiredBar />;
  // OK (NEW)
  <DivWithRequiredBar bar="3" />;
  // OK. Can still provide foo if we want
  <DivWithRequiredBar foo={3} bar="3" />;
  // @ts-expect-error foo must be a number
  <DivWithRequiredBar foo="3" bar="3" />;

The part marked as (NEW) is made possible by this PR. Once foo is provided through the .attrs call, the resulting component (DivWithRequiredBar) no longer requires foo as a prop.

Implementation

To achieve this, the Styled interface now has distinction between Outer props and Inner props:

  const DivWithRequiredBar = styled(DivWithRequiredFooBar).attrs({ foo: 42 })`
    // ↓ `props` here is the inner props which have undergone the defaulting,
    // so `props.foo` is always supplied (not optional)
    margin; ${props => props.foo * 10}px;
  `;
  // User of the resulting component sees the outer props,
  // so foo is optional here
  <DivWithRequiredBar bar="3" />;

Thus the Styled interface now has new InnerProps type attribute. It is usually same as OuterProps, but the updated attrs() types can make them diverge.

Copy link
Contributor

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

This looks really good!

@quantizor quantizor merged commit 028628b into styled-components:main Apr 30, 2024
5 checks passed
@uhyo uhyo deleted the fix-4076 branch May 1, 2024 01:29
kodiakhq bot pushed a commit to X-oss-byte/Nextjs that referenced this pull request May 1, 2024
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [styled-components](https://styled-components.com) ([source](https://togithub.com/styled-components/styled-components)) | [`6.1.8` -> `6.1.9`](https://renovatebot.com/diffs/npm/styled-components/6.1.8/6.1.9) | [![age](https://developer.mend.io/api/mc/badges/age/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>styled-components/styled-components (styled-components)</summary>

### [`v6.1.9`](https://togithub.com/styled-components/styled-components/releases/tag/v6.1.9)

[Compare Source](https://togithub.com/styled-components/styled-components/compare/v6.1.8...v6.1.9)

#### What's Changed

-   fix: improve types for .attrs() by [@&#8203;uhyo](https://togithub.com/uhyo) in [styled-components/styled-components#4288
-   fix(types): allow using a styled component as a key inside object styles in the web runtime by [@&#8203;iiroj](https://togithub.com/iiroj) in [styled-components/styled-components#4249
-   Upgrading babel dependencies to latest to fix CVE-2023-45133 by [@&#8203;JesseObrien](https://togithub.com/JesseObrien) in [styled-components/styled-components#4214

#### New Contributors

-   [@&#8203;uhyo](https://togithub.com/uhyo) made their first contribution in [styled-components/styled-components#4288
-   [@&#8203;iiroj](https://togithub.com/iiroj) made their first contribution in [styled-components/styled-components#4249
-   [@&#8203;JesseObrien](https://togithub.com/JesseObrien) made their first contribution in [styled-components/styled-components#4214

**Full Changelog**: styled-components/styled-components@v6.1.8...v6.1.9

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/X-oss-byte/Nextjs).
github-merge-queue bot pushed a commit to camunda/zeebe that referenced this pull request May 1, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [styled-components](https://styled-components.com)
([source](https://togithub.com/styled-components/styled-components)) |
[`6.1.8` ->
`6.1.9`](https://renovatebot.com/diffs/npm/styled-components/6.1.8/6.1.9)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>styled-components/styled-components
(styled-components)</summary>

###
[`v6.1.9`](https://togithub.com/styled-components/styled-components/releases/tag/v6.1.9)

[Compare
Source](https://togithub.com/styled-components/styled-components/compare/v6.1.8...v6.1.9)

#### What's Changed

- fix: improve types for .attrs() by
[@&#8203;uhyo](https://togithub.com/uhyo) in
[styled-components/styled-components#4288
- fix(types): allow using a styled component as a key inside object
styles in the web runtime by [@&#8203;iiroj](https://togithub.com/iiroj)
in
[styled-components/styled-components#4249
- Upgrading babel dependencies to latest to fix CVE-2023-45133 by
[@&#8203;JesseObrien](https://togithub.com/JesseObrien) in
[styled-components/styled-components#4214

#### New Contributors

- [@&#8203;uhyo](https://togithub.com/uhyo) made their first
contribution in
[styled-components/styled-components#4288
- [@&#8203;iiroj](https://togithub.com/iiroj) made their first
contribution in
[styled-components/styled-components#4249
- [@&#8203;JesseObrien](https://togithub.com/JesseObrien) made their
first contribution in
[styled-components/styled-components#4214

**Full Changelog**:
styled-components/styled-components@v6.1.8...v6.1.9

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/camunda/zeebe).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMjEuMiIsInVwZGF0ZWRJblZlciI6IjM3LjMyMS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJhdXRvbWVyZ2UiXX0=-->
renovate bot added a commit to camunda/zeebe that referenced this pull request May 2, 2024
….5) (#18133)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [styled-components](https://styled-components.com)
([source](https://togithub.com/styled-components/styled-components)) |
[`6.1.8` ->
`6.1.9`](https://renovatebot.com/diffs/npm/styled-components/6.1.8/6.1.9)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/styled-components/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/styled-components/6.1.8/6.1.9?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>styled-components/styled-components
(styled-components)</summary>

###
[`v6.1.9`](https://togithub.com/styled-components/styled-components/releases/tag/v6.1.9)

[Compare
Source](https://togithub.com/styled-components/styled-components/compare/v6.1.8...v6.1.9)

#### What's Changed

- fix: improve types for .attrs() by
[@&#8203;uhyo](https://togithub.com/uhyo) in
[styled-components/styled-components#4288
- fix(types): allow using a styled component as a key inside object
styles in the web runtime by [@&#8203;iiroj](https://togithub.com/iiroj)
in
[styled-components/styled-components#4249
- Upgrading babel dependencies to latest to fix CVE-2023-45133 by
[@&#8203;JesseObrien](https://togithub.com/JesseObrien) in
[styled-components/styled-components#4214

#### New Contributors

- [@&#8203;uhyo](https://togithub.com/uhyo) made their first
contribution in
[styled-components/styled-components#4288
- [@&#8203;iiroj](https://togithub.com/iiroj) made their first
contribution in
[styled-components/styled-components#4249
- [@&#8203;JesseObrien](https://togithub.com/JesseObrien) made their
first contribution in
[styled-components/styled-components#4214

**Full Changelog**:
styled-components/styled-components@v6.1.8...v6.1.9

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/camunda/zeebe).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMjEuMiIsInVwZGF0ZWRJblZlciI6IjM3LjMyMS4yIiwidGFyZ2V0QnJhbmNoIjoic3RhYmxlL29wZXJhdGUtOC41IiwibGFiZWxzIjpbImF1dG9tZXJnZSJdfQ==-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants