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.
Fixes in this PR get the typing closer to what React's testing library does and thereby makes migrations from React to Preact easier. When I migrated from React to Preact, a TS error appeared as soon as I imported
renderHook
from the Preact testing library:The error says that
result.current
could possibly be undefined, meaning I need to unfortunately refactorresult.current(yo)
toresult.current?.(yo)
.At first this issue makes sense because the resultContainer function has an
initialValue
argument that might be undefined and also the value oflet value
might be undefined. However, the library actually never calls resultContainer with an argument, justifying the removal ofinitialValue
as a potential argument.That doesn't solve the potential undefined value though. For this, I'm following the pattern in React's resultContainer by assigning
value as R
. This way types are safe.I verified type-safety locally in one of the tests. When I change…
…to…
…TS throws an error for
result.current.count
, saying thatresult.current
can't have a propertycount
– which is correct becauseresult.current
is now of typestring
.