From 020c67b8a027bdf3a2b036d0371cdba60628cb46 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 30 Nov 2022 10:46:01 +0100 Subject: [PATCH] useControlledValue: let TypeScript infer the return type (#46164) * useControlledValue: let TypeScript infer the return type * CHANGELOG --- packages/components/CHANGELOG.md | 1 + packages/components/src/utils/hooks/use-controlled-value.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 31ff26cd34b837..00c1aea07ed14c 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -22,6 +22,7 @@ ### Internal +- `useControlledValue`: let TypeScript infer the return type ([#46164](https://github.com/WordPress/gutenberg/pull/46164)) - `LinkedButton`: remove unnecessary `span` tag ([#46063](https://github.com/WordPress/gutenberg/pull/46063)) - NumberControl: refactor styles/tests/stories to TypeScript, replace fireEvent with user-event ([#45990](https://github.com/WordPress/gutenberg/pull/45990)). - `useBaseField`: Convert to TypeScript ([#45712](https://github.com/WordPress/gutenberg/pull/45712)). diff --git a/packages/components/src/utils/hooks/use-controlled-value.ts b/packages/components/src/utils/hooks/use-controlled-value.ts index 5a3824d465c07c..bb7f3aea805c99 100644 --- a/packages/components/src/utils/hooks/use-controlled-value.ts +++ b/packages/components/src/utils/hooks/use-controlled-value.ts @@ -22,7 +22,7 @@ export function useControlledValue< T >( { defaultValue, onChange, value: valueProp, -}: Props< T > ): [ T | undefined, ( value: T ) => void ] { +}: Props< T > ) { const hasValue = typeof valueProp !== 'undefined'; const initialValue = hasValue ? valueProp : defaultValue; const [ state, setState ] = useState( initialValue ); @@ -40,5 +40,5 @@ export function useControlledValue< T >( { setValue = setState; } - return [ value, setValue ]; + return [ value, setValue as typeof setState ] as const; }