[WIP] useGenericObjectState: Fix types so that setPropValue function narrows value type by key #2994
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Standalone, semi-related to RHOAIENG-8454Would resolve RHOAIENG-9748 if completed. Closing the PR for now because fixing the errors is out of the scope of the little "sneak in a tiny change" time I budgeted.
Description
I was helping @gitdallas look into using
useGenericObjectState
for RHOAIENG-8454 and I noticed it had this weird type quirk. I played around for a minute to see if it would be easy to fix and this very small fix worked fine, so I figured I should open a PR.useGenericObjectState<T>
returns (in part) a function of typeUpdateObjectAtPropAndValue<T>
which takes a key and value and updates the value at that key in the state objectT
. The current type of the argumentpropValue: ValueOf<T>
means that no matter whatpropKey
is passed in, TypeScript only enforces that your value is one of any of the valid property values for any key on the object. This means potential run time type errors, as I can see playing around with the usage inuseRunFormData
:With this change applied, the type of
propValue
is narrowed to the actual type of the property matching thepropKey
you pass in.Here's that same line with this change applied, correctly showing an error:
And if I change the key to the one that matches the value I'm passing, the error goes away:
How Has This Been Tested?
Test Impact
Request review criteria:
Self checklist (all need to be checked):
If you have UI changes:
After the PR is posted & before it merges:
main