Search code examples
typescripttypesinterfacetyping

TypeScript define interface for "as X"


I have the following code:

const [surveyResponses, totalCount]: [SurveyResponse[], number] =
await this.someRepo.findByFilters(filter) as [SurveyResponse[], number];

Is it possible to give the as [SurveyResponse[], number] portion an interface? The findByFilters function returns either a paginated result or not, so I'd like to have an interface for each.


Solution

  • Not an interface but a type alias, yes:

    type X = [SurveyResponse[], number];
    
    const [surveyResponses, totalCount]: X = ...
    await this.someRepo.findByFilters(filter) as X;