简体   繁体   中英

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.

Not an interface but a type alias, yes:

type X = [SurveyResponse[], number];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM