Skip to content

Commit b641a05

Browse files
authored
Components: Fix TS types for isValueDefined()/isValueEmpty() (#43983)
* Components: Fix TS types for isValueEmpty() * Add changelog
1 parent eadbca1 commit b641a05

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

packages/components/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
### Internal
3131

32+
- Fix TypeScript types for `isValueDefined()` and `isValueEmpty()` utility functions ([#43983](https://github.com/WordPress/gutenberg/pull/43983)).
3233
- `RadioControl`: Clean up styles to use less custom CSS ([#43868](https://github.com/WordPress/gutenberg/pull/43868)).
3334
- Remove unused `normalizeArrowKey` utility function ([#43640](https://github.com/WordPress/gutenberg/pull/43640/)).
3435
- `SearchControl`: Convert to TypeScript ([#43871](https://github.com/WordPress/gutenberg/pull/43871)).

packages/components/src/number-control/index.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ function UnforwardedNumberControl(
4343
const isStepAny = step === 'any';
4444
const baseStep = isStepAny ? 1 : ensureNumber( step );
4545
const baseValue = roundClamp( 0, min, max, baseStep );
46-
const constrainValue = ( value: number, stepOverride?: number ) => {
46+
const constrainValue = (
47+
value: number | string,
48+
stepOverride?: number
49+
) => {
4750
// When step is "any" clamp the value, otherwise round and clamp it.
4851
return isStepAny
49-
? Math.min( max, Math.max( min, value ) )
52+
? Math.min( max, Math.max( min, ensureNumber( value ) ) )
5053
: roundClamp( value, min, max, stepOverride ?? baseStep );
5154
};
5255

@@ -91,18 +94,15 @@ function UnforwardedNumberControl(
9194
}
9295

9396
if ( type === inputControlActionTypes.PRESS_UP ) {
94-
// @ts-expect-error TODO: isValueEmpty() needs to be typed properly
9597
nextValue = add( nextValue, incrementalValue );
9698
}
9799

98100
if ( type === inputControlActionTypes.PRESS_DOWN ) {
99-
// @ts-expect-error TODO: isValueEmpty() needs to be typed properly
100101
nextValue = subtract( nextValue, incrementalValue );
101102
}
102103

103104
// @ts-expect-error TODO: Resolve discrepancy between `value` types in InputControl based components
104105
nextState.value = constrainValue(
105-
// @ts-expect-error TODO: isValueEmpty() needs to be typed properly
106106
nextValue,
107107
enableShift ? incrementalValue : undefined
108108
);
@@ -151,7 +151,7 @@ function UnforwardedNumberControl(
151151

152152
// @ts-expect-error TODO: Resolve discrepancy between `value` types in InputControl based components
153153
nextState.value = constrainValue(
154-
// @ts-expect-error TODO: isValueEmpty() needs to be typed properly
154+
// @ts-expect-error TODO: Investigate if it's ok for currentValue to be undefined
155155
add( currentValue, distance ),
156156
enableShift ? modifier : undefined
157157
);
@@ -171,7 +171,7 @@ function UnforwardedNumberControl(
171171
// @ts-expect-error TODO: Resolve discrepancy between `value` types in InputControl based components
172172
nextState.value = applyEmptyValue
173173
? currentValue
174-
: // @ts-expect-error TODO: isValueEmpty() needs to be typed properly
174+
: // @ts-expect-error TODO: Investigate if it's ok for currentValue to be undefined
175175
constrainValue( currentValue );
176176
}
177177

packages/components/src/utils/math.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export function clamp( value, min, max ) {
7373
/**
7474
* Clamps a value based on a min/max range with rounding
7575
*
76-
* @param {number} value The value.
77-
* @param {number} min The minimum range.
78-
* @param {number} max The maximum range.
79-
* @param {number} step A multiplier for the value.
76+
* @param {number | string} value The value.
77+
* @param {number} min The minimum range.
78+
* @param {number} max The maximum range.
79+
* @param {number} step A multiplier for the value.
8080
*
8181
* @return {number} The rounded and clamped value.
8282
*/

packages/components/src/utils/values.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* @template T
66
*
7-
* @param {T | null | undefined} value The value to check.
8-
* @return {value is T} Whether value is not null or undefined.
7+
* @param {T} value The value to check.
8+
* @return {value is Exclude<T, null | undefined>} Whether value is not null or undefined.
99
*/
1010
export function isValueDefined( value ) {
1111
return value !== undefined && value !== null;
@@ -16,10 +16,8 @@ export function isValueDefined( value ) {
1616
/**
1717
* Determines if a value is empty, null, or undefined.
1818
*
19-
* @template T
20-
*
21-
* @param {T | "" | null | undefined} value The value to check.
22-
* @return {value is T} Whether value is empty.
19+
* @param {string | number | null | undefined} value The value to check.
20+
* @return {value is ("" | null | undefined)} Whether value is empty.
2321
*/
2422
export function isValueEmpty( value ) {
2523
const isEmptyString = value === '';

0 commit comments

Comments
 (0)