Skip to content

Instantly share code, notes, and snippets.

@dewey92
Created May 8, 2019 06:22
Show Gist options
  • Select an option

  • Save dewey92/6e7eb8d0df4d4f41be12b1655f691723 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/6e7eb8d0df4d4f41be12b1655f691723 to your computer and use it in GitHub Desktop.
Generics Remote Data
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