react-promise
react-promise copied to clipboard
Improve specificity of return type
Increases the specificity of the return type. This allows consumers to make assumptions about the existence of value
based on the value/presence of loading
and error
.
For example, this will produce a type error with the current version of react-promise
, but will work as expected with these changes:
function processMessage(input: string): string;
const messagePromise: Promise<string> = doXHRGetString();
const asyncMessage = usePromise(messagePromise);
const displayValue = asyncMessage.loading
? 'loading'
: asyncMessage.error
? asyncMessage.error.toString()
: processMessage(asyncMessage.value); // type error here with current version b/c `value` is type `string | undefined`