react-promise icon indicating copy to clipboard operation
react-promise copied to clipboard

Improve specificity of return type

Open nickiaconis opened this issue 3 years ago • 0 comments

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`

nickiaconis avatar Jul 23 '21 18:07 nickiaconis