Bug Report
🔎 Search Terms
array, union, callback, any, contextual
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed all FAQ
⏯ Playground Link
Playground link with relevant code
💻 Code
letmyCondition=true;constarr1=["a","b","c"];constarr2=[1,2,3];// Correctly inferred as string[] | number[]constmyArr=myCondition ? arr1 : arr2;for(constvalofmyArr){// val correctly inferred to string | numberconsole.log(val);}myArr.forEach((val)=>{// val incorrectly inferred as anyconsole.log(val);});🙁 Actual behavior
The for..of loop correctly infers the type, but array methods (e.g. forEach, map, filter, etc.) infer the array item type to be any.
🙂 Expected behavior
Given a type which is the union of arrays and/or tuples, T1[] | T2[] | ... Tn[], array methods which take functions should infer the item parameter's type as T1[number] | T2[number] | ... Tn[number].
Bug Report
🔎 Search Terms
array, union, callback, any, contextual
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The
for..ofloop correctly infers the type, but array methods (e.g.forEach,map,filter, etc.) infer the array item type to beany.🙂 Expected behavior
Given a type which is the union of arrays and/or tuples,
T1[] | T2[] | ... Tn[], array methods which take functions should infer the item parameter's type asT1[number] | T2[number] | ... Tn[number].