Skip to content

Incorrect overload selected for Array.prototype.map(...)Β #47571

Closed
@oleg-slapdash

Description

@oleg-slapdash

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions