I have a promise all with a mixed array and I'm using a spread operator to split the output in 2 variables, the first as IUnpackedFile and the rest as IDescriptionBody[]. The way I solved it is casting the 2 variables using the as keyword. Code below:
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
);
Why can't I type casting the output using a tuple?
then(([bufferArray, ...rest]: [IUnpackedFile , IDescriptionBody[]])
This is the error I'm getting:
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)[]'.
EDIT: so far I've found this: Proposal: Variadic Kinds Basically it is a proposal that has been in discussion since 30 Oct 2015.