Skip to content

Callback parameter type is any for union of arrays #52748

Description

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].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions