【发布时间】:2018-10-13 06:26:06
【问题描述】:
我有一个使用混合数组的承诺,我使用扩展运算符将输出拆分为 2 个变量,第一个为 IUnpackedFile,其余为 IDescriptionBody[]。 我解决它的方法是使用 as 关键字转换 2 个变量。代码如下:
return Promise.all<IUnpackedFile | IDescriptionBody>([
unpackFromHttp("/data/withTexture/5782622127702409215.dat"),
...texturePromises
])
.then(([bufferArray, ...rest]) => {
// casting here
const description = (rest as IDescriptionBody[]).reduce(
(res, val) => {
res[val.CellId] = val;
return res;
},
{} as IDescription
);
const meshMaterials = extractMesh(
// casting here
(bufferArray as IUnpackedFile).result,
description
);
为什么我不能使用元组类型转换输出?
then(([bufferArray, ...rest]: [IUnpackedFile , IDescriptionBody[]])
这是我得到的错误:
Argument of type '([bufferArray, ...rest]: [IUnpackedFile, IDescriptionBody[]]) => void' is not assignable to parameter of type '((value: (IDescriptionBody | IUnpackedFile)[]) => void | PromiseLike<void>) | null | undefined'.
Type '([bufferArray, ...rest]: [IUnpackedFile, IDescriptionBody[]]) => void' is not assignable to type '(value: (IDescriptionBody | IUnpackedFile)[]) => void | PromiseLike<void>'.
Types of parameters '__0' and 'value' are incompatible.
Type '(IDescriptionBody | IUnpackedFile)[]' is not assignable to type '[IUnpackedFile, IDescriptionBody[]]'.
Property '0' is missing in type '(IDescriptionBody | IUnpackedFile)[]'.
编辑: 到目前为止,我发现了这个:Proposal: Variadic Kinds 基本上这是自 2015 年 10 月 30 日以来一直在讨论的提案。
【问题讨论】:
-
你使用的是哪个版本的 TypeScript?
-
"打字稿": "^2.8.3",
-
texturePromises有固定数量吗? -
很遗憾没有。
标签: typescript promise destructuring spread-syntax