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 refactor result.current(yo) to result.current?.(yo).
At first this issue makes sense because the resultContainer function has an initialValue argument that might be undefined and also the value of let value might be undefined. However, the library actually never calls resultContainer with an argument, justifying the removal of initialValue 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…
const { result } = renderHook(() => useCounter());
...
expect(result.current.count).toBe(1);
…to…
const { result } = renderHook(() => 'different');
...
expect(result.current.count).toBe(1);
…TS throws an error for result.current.count, saying that result.current can't have a property count – which is correct because result.current is now of type string.
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
.