Grokking TypeScript
0% completed
Defining Types for API Responses
The two calls at the end of this program use the same function, and the compiler reports no error for either of them. The first prints a name, and the second crashes.
interface User { id: number; name: string; email: string; } async function fetchUser(id: number): Promise<User> { const response = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`); const user: User = await response.json(); return user; } async function showName(id: number) { const user = await fetchUser(id); console.log(user.name.toUpperCase()); } showName(1)
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%