【发布时间】:2020-05-18 16:18:49
【问题描述】:
我有一个这样定义的 promises 数组。
export type PromisesArray = [
Promise<IApplicant> | null,
Promise<ICampaign | ICampaignLight> | null,
Promise<IApplication[]> | null,
Promise<IComment[]> | null,
Promise<{ status: number; message: IActionTag[] }> | null,
Promise<IHistoryEntry[]> | null,
Promise<IDocs> | null,
Promise<IForm> | null,
];
我想像const promisesArray = <PromisesArray>[]这样初始化为一个空值。
但是,我遇到了以下错误:
Conversion of type '[]' to type 'PromisesArray' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type '[]' is missing the following properties from type '[Promise<IApplicant>, Promise<ICampaign | ICampaignLight>, Promise<IApplication[]>, ... 4 more ..., Promise<...>]': 0, 1, 2, 3, and 4 more.ts(2352)
然后我像这样在数组中推送一些项目:
if (this._printOptions[EPrintOption.Tags]) {
const applicantActionsTagsPromise = ApplicantService.getActions(this._applicantId);
promisesArray.push(applicantActionsTagsPromise); // On this line
} else {
promisesArray.push(null);
}
然后遇到这个错误。
Argument of type 'Promise<IActionTag[]>' is not assignable to parameter of type 'Promise<IApplicant> | Promise<ICampaign | ICampaignLight> | Promise<IApplication[]> | ... 4 more ... | Promise<...>'.
Type 'Promise<IActionTag[]>' is not assignable to type 'Promise<IApplicant>'.
Type 'IActionTag[]' is missing the following properties from type 'IApplicant': address, advertiseid, applicantid, birthdate, and 14 more.ts(2345)
我想在不使用any 类型运行的情况下解决这个问题。
【问题讨论】:
-
ApplicantService.getActions(this._applicantId);返回什么?它似乎返回了Promise<IActionTag[]>,这不是您的类型所允许的类型之一。 -
您可能需要考虑将您的数组声明为
type PromisesArray = Promise<IApplicant | ICampaign | ICampaignLight | IApplication[] | ...>[];That would make working with the array easier。
标签: javascript typescript promise