A. Missing type assertion or validation for json before returning.
B. The fetch call should not use await.
C. The interface ApiResponse is missing a constructor.
D. The function should return void instead of Promise<ApiResponse>.
Solution
Step 1: Check how json is handled
json is returned directly without confirming it matches ApiResponse shape.
Step 2: Understand type safety requirement
TypeScript needs either a type assertion or runtime validation to ensure json fits ApiResponse.
Final Answer:
Missing type assertion or validation for json before returning. -> Option A
Quick Check:
Validate json shape before return [OK]
Hint: Always validate or assert API response type [OK]
Common Mistakes:
Assuming fetch json is automatically typed
Ignoring need for runtime checks
Misunderstanding async/await usage
5. You want to create a type-safe function that fetches a list of users from an API and returns only users with an active status. Which approach correctly applies type-safe API response handling?
hard
A. Define interface User with 'active' boolean, fetch data as Promise, then filter users by active before returning.
B. Fetch data as Promise<any>, filter users by active without type checking, then cast to User[].
C. Define interface User without 'active', fetch data as Promise, then filter users by active.
D. Fetch data as Promise, return data without filtering to keep type safety.
Solution
Step 1: Define User interface including 'active' property
To filter by active status, the User type must include an 'active' boolean field.
Step 2: Fetch data as Promise<User[]> and filter
Fetching with correct type ensures type safety; filtering users by active keeps the result accurate and typed.
Final Answer:
Define interface User with 'active' boolean, fetch data as Promise<User[]>, then filter users by active before returning. -> Option A
Quick Check:
Correct interface + typed fetch + filter = Define interface User with 'active' boolean, fetch data as Promise, then filter users by active before returning. [OK]
Hint: Include all needed fields in interface before filtering [OK]
Common Mistakes:
Casting any to User[] without checks
Filtering on missing properties in interface
Returning unfiltered data losing requirements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.