Skip to content

useControlledValue: let TypeScript infer the return type#46164

Merged
ciampo merged 2 commits into
trunkfrom
refactor/use-controlled-value-refine-type
Nov 30, 2022
Merged

useControlledValue: let TypeScript infer the return type#46164
ciampo merged 2 commits into
trunkfrom
refactor/use-controlled-value-refine-type

Conversation

@ciampo

@ciampo ciampo commented Nov 29, 2022

Copy link
Copy Markdown
Contributor

What?

Refine useControlledValue's return type

Why?

The new type makes the hook behave more similarly to a native react hook, avoiding the need for type casting like discussed in #45771 (comment)

How?

  • in the return statement, we typecast setValue to be "whatever is the type of the setState function
  • we add as const, which basically tells TypeScript to interpret the type of the return statement as literally as possible (see const assertions)

Testing Instructions

  • code makes sense
  • no TypeScript errors

@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Package] Components /packages/components labels Nov 29, 2022
@ciampo ciampo requested a review from ajitbohra as a code owner November 29, 2022 15:27
@ciampo ciampo self-assigned this Nov 29, 2022

@sarayourfriend sarayourfriend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense to me 🙂 It is a nice simplification.

@ciampo ciampo enabled auto-merge (squash) November 29, 2022 22:48

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good 🚀

@ciampo ciampo merged commit 8633ae8 into trunk Nov 30, 2022
@ciampo ciampo deleted the refactor/use-controlled-value-refine-type branch November 30, 2022 09:46
@github-actions github-actions Bot added this to the Gutenberg 14.7 milestone Nov 30, 2022
mpkelly pushed a commit to mpkelly/gutenberg that referenced this pull request Dec 7, 2022
…6164)

* useControlledValue: let TypeScript infer the return type

* CHANGELOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Components /packages/components [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants