-
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
Incorrect handling of unions of arrays. #12829
Comments
Your first example compiles because control flow analysis has determined the type of |
This is the smallest example I could create to recreate an issue. In channelEncoding.forEach(function(fieldDef: FieldDef | OrderChannelDef) {
fields.push(field(fieldDef));
}); I know that But I get the following error [ts] Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: FieldDef, index: number, array: FieldDef[]) => void, thisArg?: any) => void...' has no compatible call signatures. Yes, the error is slightly different but I thought that was because ts handles complex types differently from simple types. I thought this error is caused by the behavior of the flow analysis (which is being smarter than my explicit type annotations). |
This error is reported because (channelEncoding as (FieldDef | OrderChannelDef)[]).forEach(...) Alternatively, you could change |
For more discussion see #10620. |
Thanks for the explanation! The behavior I posted above is still a bit odd (flow analysis overrides my explicit annotation) but I don't have a problem with it. |
TypeScript Version: 2.1
Code
This code works although the type of the forEach callback is not suffuient.
But this fails (which is expected)
I'd expect only this code to be valid
I understand that
boolean[] | number[]
is a subtype of(boolean | number)[]
and would expect typescript to be stricter if I provide the former.The text was updated successfully, but these errors were encountered: