【发布时间】:2019-10-11 02:03:16
【问题描述】:
我有以下代码:
const [surveyResponses, totalCount]: [SurveyResponse[], number] =
await this.someRepo.findByFilters(filter) as [SurveyResponse[], number];
是否可以为as [SurveyResponse[], number] 部分提供接口? findByFilters 函数返回或不返回分页结果,所以我想为每个结果提供一个接口。
【问题讨论】:
-
请注意,您不需要两个
[SurveyResponse[], number]注释,只需要一个或另一个。 -
不,findByFilters 要么只返回一个 SurveyResponse 数组,要么返回问题中的数组。 tsc 如果我没有定义它会抛出一个错误。
-
不,你不需要两者。
as断言足以定义类型,或者如果它可以分配给带有注释的元组类型,则您不需要断言,但如果不是,您当然不需要 both,只需断言。
标签: typescript types interface typing