question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Incorrect overload selected for `Array.prototype.map(...)`

See original GitHub issue

Bug Report

๐Ÿ”Ž Search Terms

overload, array.prototype.map

๐Ÿ•— Version & Regression Information

  • This is the behavior in every version I tried.

โฏ Playground Link

Playground link with relevant code

๐Ÿ’ป Code

declare type Obj = {a: true};
declare const arr: Array<string | Obj>;
declare function func(url: string | Obj): string;
declare function func<T extends Obj | string>(url: T): string | null;
const fails: string[] = arr.map(func);
// ^^^ Type '(string | null)[]' is not assignable to type 'string[]'.
const works: string[] = arr.map((v) => func(v));



interface CustomArray<T> {
    map<U>(callbackfn: (value: T) => U): U[];
}
declare const arr2: CustomArray<string | Obj>;
const fails2: string[] = arr2.map(func);
// ^^^ Type '(string | null)[]' is not assignable to type 'string[]'.
const works2: string[] = arr2.map((v) => func(v));

๐Ÿ™ Actual behavior

It fails to select the correct overloard.

๐Ÿ™‚ Expected behavior

It is supposed to select the correct overload.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
oleg-slapdashcommented, Jan 25, 2022

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

type ObjWithFoo = {foo: string};

declare function getFoo(foo: string | ObjWithFoo): string;
declare function getFoo(url: ObjWithFoo | string | null | undefined): string | null | undefined;

declare const arr: Array<string | ObjWithFoo>;

export const works: string[] = arr.map((v) => getFoo(v));
export const fails: string[] = arr.map(getFoo);
// Err: ^^^ Type '(string | null | undefined)[]' is not assignable to type 'string[]'
0reactions
maple5233commented, Apr 26, 2022

Conditional types will get instantiated, so Iโ€™d recommend this:

type ObjWithFoo = {foo: string};

declare function getFoo<T extends ObjWithFoo | string | null | undefined>(url: T): T extends undefined | null ? T : string;

declare const arr: Array<string | ObjWithFoo>;

export const works: string[] = arr.map((v) => getFoo(v));
export const alsoWorks: string[] = arr.map(getFoo);

declare const arrU: Array<string | ObjWithFoo | undefined>;
export const chk = arrU.map(getFoo); // Array<string | undefined>

Will this bug be fixed? The way using the conditional types looks like a work-around.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Array.prototype.map declaration - Stack Overflow
The array method overloads you object against in Array<T> introduce a statically typed this parameter in the event the array is an actual...
Read more >
Array.prototype.map() - JavaScript - MDN Web Docs
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array....
Read more >
Stop abusing .map()! - DEV Community โ€ โ€
map () . When operating on small arrays, it won't cause any hurt. But when we make this mistake with a bigger array...
Read more >
Overloading New and Delete operator in c++ - GeeksforGeeks
If these operators are overloaded using member function for a class, it means that these operators are overloaded only for that specific class....
Read more >
Operator Overloading, C++ FAQ - Standard C++
How do I create a subscript operator for a Matrix class? Why shouldn't my Matrix class's interface look like an array-of-array? I still...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found