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
Incorrect overload selected for Array.prototype.map(...)Β #47571
declaretypeObj={a: true};declareconstarr: Array<string|Obj>;declarefunctionfunc(url: string|Obj): string;declarefunctionfunc<TextendsObj|string>(url: T): string|null;constfails: string[]=arr.map(func);// ^^^ Type '(string | null)[]' is not assignable to type 'string[]'.constworks: string[]=arr.map((v)=>func(v));interfaceCustomArray<T>{map<U>(callbackfn: (value: T)=>U): U[];}declareconstarr2: CustomArray<string|Obj>;constfails2: string[]=arr2.map(func);// ^^^ Type '(string | null)[]' is not assignable to type 'string[]'.constworks2: string[]=arr2.map((v)=>func(v));
Ryan Cavanaugh (@RyanCavanaugh), this is an approximation. Basically, this is a helper function that gets a string property of an object or returns a string if a string is passed instead of an object.
typeObjWithFoo={foo: string};declarefunctiongetFoo(foo: string|ObjWithFoo): string;declarefunctiongetFoo(url: ObjWithFoo|string|null|undefined): string|null|undefined;declareconstarr: Array<string|ObjWithFoo>;exportconstworks: string[]=arr.map((v)=>getFoo(v));exportconstfails: string[]=arr.map(getFoo);// Err: ^^^ Type '(string | null | undefined)[]' is not assignable to type 'string[]'
It's specifically the case that whenever an overloaded function is passed as an argument to another function, its last overload signature is treated as its signature and all preceding signatures are ignored. There is something of a logic to this in that the last signature will sometimes be a "catch-all" signature and thus most likely to not cause a type error (though for an overloaded function without a catch-all signature, this choice ends up being effectively completely arbitrary).
Activity
RyanCavanaugh commentedon Jan 25, 2022
Inference doesn't do normal overload resolution per se, instead it performs structural inference between the signatures to try to line things up.
Is there a real definition of
func? This problem can almost always be avoided in non-pathological cases.oleg-slapdash commentedon Jan 25, 2022
Ryan Cavanaugh (@RyanCavanaugh), this is an approximation. Basically, this is a helper function that gets a string property of an object or returns a string if a string is passed instead of an object.
RyanCavanaugh commentedon Jan 25, 2022
Conditional types will get instantiated, so I'd recommend this:
oleg-slapdash commentedon Jan 25, 2022
Ryan Cavanaugh (@RyanCavanaugh) thanks!
maple5233 commentedon Apr 26, 2022
Will this bug be fixed? The way using the conditional types looks like a work-around.
ExplodingCabbage commentedon Mar 14, 2025
Summary of others' points from duplicate #61398, for the benefit of anyone else landing here from search: