Created
November 19, 2021 19:09
-
-
Save mendes5/c94aa1275fda7b34aadac60be284a2a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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