Incorrect overload selected for `Array.prototype.map(...)`
See original GitHub issueBug 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
Will this bug be fixed? The way using the conditional types looks like a work-around.