-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inference of [a,b] to union type array is missing index signature [0]:A and [1]:B #9216
Comments
it's a well known annoying problem with tuples among many others, a workaround i use:
|
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, |
Could it infer type to |
Seems to be effective: const a = 1;
const b = 'string';
const p: (number | string)[] & [ number, string ] = [a, b];
function all<T1, T2>(values: [T1, T2]): [T1, T2] {
return;
}
function some<T>(values: T[]): T {
return;
}
const r1 = all(p);
const r2 = some(p); |
If you inferred that, you wouldn't be able to assign a |
When calling a method declared as
all<T1, T2>(values: [T1, T2]);
I cannot use a union type array.TypeScript Version:
1.8.10
Code
OK:
Error:
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 toBtw. the error message is rather confusing, since
(A|B)[]
actually has an index signature.The text was updated successfully, but these errors were encountered: