Created
May 8, 2019 06:22
-
-
Save dewey92/6e7eb8d0df4d4f41be12b1655f691723 to your computer and use it in GitHub Desktop.
Generics Remote Data
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
| interface RemoteData<T> { | |
| data: T; | |
| status: 'IDLE' | 'FETCHING' | 'SUCCESS' | 'ERROR' | |
| } | |
| const createRemoteData = <T>(data: T): RemoteData<T> => ({ | |
| data, | |
| status: 'IDLE', | |
| }); | |
| // -- examples -- | |
| interface User { | |
| id: string; | |
| name: string | |
| } | |
| createRemoteData('1') | |
| // { data: '1', status: 'IDLE' } | |
| // tipenya `RemoteData<string>` | |
| createRemoteData<User[]>([]) | |
| // { data: [], status: 'IDLE' }, | |
| // tipenya `RemoteData<User[]>` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment