API Response Handler with Generic Types

Medium

Type an API response handler for Hack Frontend. The function should accept a URL and return a Promise with typed data.

Requirements:

  • Generic function fetchHackFrontendAPI<T>
  • Returns Promise<ApiResponse<T>>
  • ApiResponse has success, data or error
  • Types should narrow correctly via type guards

Examples:

Input 1: fetchHackFrontendAPI<User>("/api/user")
Output 1: Promise<ApiResponse<User>>
Output
Run your code to see results.

By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.