Question Details

No question body available.

Tags

typescript

Answers (2)

Accepted Answer Available
Accepted Answer
July 7, 2026 Score: 10 Rep: 949,633 Quality: Expert Completeness: 80%

why would i need to type the return value although i told typescript what will the function return?

You've told TypeScript that fetchCashTeams returns Promise.

You then said return response;

Therefore TypeScript checks that response is Promise and it isn't. It is Promise.

This is a check to prevent you from returning an invalid value.

response is Promise because httpRequest returns Promise and you didn't give it any hint as to what T is.

The simple way around this is to be explicit about that: httpRequest("/api/cash-teams", "GET");


This does mean that you are writing an assumption that you will get data in that format back, so you could make it more robust by using a guard function:

function isTeamTypeArray(value: any): value is TeamType[] {
   return Array.isArray(value) && 
       value.every(
           (team) => ('someField' in team && 'someOtherField' in team)
       );
}

export async function fetchCashTeams(): Promise { const response = await httpRequest("/api/cash-teams", "GET"); if (isTeamTypeArray(response)) { return response; } throw new Error("Value returned from web service was not a TeamType[]"); }

That is just a simple example of a Type Guard, you could make a more robust one by validating using a JSON Schema library.

July 8, 2026 Score: 3 Rep: 82 Quality: Low Completeness: 50%

httpRequest is a generic function, but TypeScript can't infer T because it only appears in the return type. Therefore, response is inferred as unknown.

Specify the generic type explicitly:

export async function fetchCashTeams(): Promise {
  return httpRequest("/api/cash-teams", "GET");
}

Promise only defines what fetchCashTeams must return—it doesn't tell httpRequest what T is.