Skip to content

Instantly share code, notes, and snippets.

@strboul
Created May 25, 2021 09:31
Show Gist options
  • Save strboul/6ef31da34dedf9936a105a05f35002b4 to your computer and use it in GitHub Desktop.
Save strboul/6ef31da34dedf9936a105a05f35002b4 to your computer and use it in GitHub Desktop.
// We parameterize ApiResponse to specify the type of its data field
interface ApiResponse<T> {
status: "success" | "failure";
data: T;
}
interface User {
id: number
name: string
username: string
email: string
phone: string
}
const getUsers = async (id: number): Promise<ApiResponse<User[]>> => {
const users = await fetch(`https://jsonplaceholder.typicode.com/users/${1}`)
return users.json()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment