This question is focused on Typescript usage in React.
I have two React hooks: useStatus(): Status and useResult(): Result | null and use them like:
const status = useStatus();
const result = useResult();
if (status !== "success") {
return <Loading />;
}
return ... something using result ...;
I can change the signature of both functions (parameters and return type).
I can't change the position of the useResult call (it has to be before if) - that's React's only constraint.
Conceptually, I know result will not be null if status is "success". I'd like to somehow encode this information in return type. That way, Typescript would somehow know at last line, that status is "success" so result is Result (not Result | null).
I know I can separate in two different components - that's what I'm already doing, but curious if I can avoid that.
Is there a way to achieve this?
Thanks! :)