You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inference of [a,b] to union type array is missing index signature [0]:A and [1]:B #9216
Expected behavior:
It would be nice if both versions work, without explicitly reference types A and B. I think the problem is, that [a, b] infers to (A|B)[] instead of [A,B], which should prevent me from [a,b].push(new C()) (Is this the intension?). The problem is, that (A|B)[] looses the indexers [0]: A and [1]: B. I think that's what the error message wants to say?
Would it be possible to infer [a, b] to something like (A|B)[A, B] which is compatible to
interfaceIAB{[i:number]: A|B;[0]:A;[1]:B;}
Btw. the error message is rather confusing, since (A|B)[] actually has an index signature.
it's a well known annoying problem with tuples among many others,
it doesn't work the way you expect because array literals are inferred being array-types not tuple-types
a workaround i use:
function of2<a, b>(one: a, two: b) : [a, b] {
return [one, two];
}
...
var promises = of2(a, b);
$q.all(promises).then(/*.. here comes tuple ..*/);
As noted by Aleksey-Bykov, the compiler can not infer any array literal as a tuple, as this would be a massive breaking change. so by the time we get to the call, promises is just an array or variable length.
Activity
zpdDG4gta8XKpMCd commentedon Jun 16, 2016
it's a well known annoying problem with tuples among many others,
it doesn't work the way you expect because array literals are inferred being array-types not tuple-types
a workaround i use:
mhegazy commentedon Jun 16, 2016
As noted by Aleksey-Bykov, the compiler can not infer any array literal as a tuple, as this would be a massive breaking change. so by the time we get to the call,
promisesis just an array or variable length.iskiselev commentedon Jun 17, 2016
Could it infer type to
(A|B)[] & [A,B]which should not be a breaking change? Or have I missed something?kitsonk commentedon Jun 17, 2016
Seems to be effective:
DanielRosenwasser commentedon Jun 18, 2016
If you inferred that, you wouldn't be able to assign a
stringtop[0], which(number | string)[]does allow.