Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created November 19, 2021 19:09
Show Gist options
  • Select an option

  • Save mendes5/c94aa1275fda7b34aadac60be284a2a3 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/c94aa1275fda7b34aadac60be284a2a3 to your computer and use it in GitHub Desktop.
type Loaded<T> = {
state: 'loaded',
data: unknown,
};
type ErrorState = {
state: 'error',
error: Error,
};
type Loading = {
state: 'loading',
since: number,
};
type APIState<T> = Loaded<T> | ErrorState | Loading;
function getResult<T>(): APIState<T> {
return { state: 'error', error: new Error('') };
};
const a = getResult();
if (a.state === 'loaded') {
console.log(a.data);
} else if (a.state === 'error') {
console.log(a.error);
} else {
console.log(a.since);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment