简体   繁体   English

TypeScript为“ as X”定义接口

[英]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? 是否可以给as [SurveyResponse[], number]部分提供接口? The findByFilters function returns either a paginated result or not, so I'd like to have an interface for each. findByFilters函数是否返回分页结果,所以我想为每个接口都有一个接口。

Not an interface but a type alias, yes: 不是接口,而是类型别名,是的:

type X = [SurveyResponse[], number];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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