Skip to content

Overload selection differs for mapped types vs interfaces #25378

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 2.9.1

Search Terms: overload mapped types interface

Code

interface LoDashStatic {
  mapValues<T, TResult>(
      obj: { [index: string]: T },
      callback: (t: T) => TResult,
  ): { [index: string]: TResult };
  mapValues<T extends object, TResult>(
      obj: T,
      callback: (t: T[keyof T]) => TResult,
  ): { [P in keyof T]: TResult };
}

declare const _: LoDashStatic;

interface AbcObjectInterface {
  a: number;
  b: string;
  c: boolean;
}
type AbcObjectRecord = Record<"a" | "b" | "c", string>;

declare const abcObjectRecord: AbcObjectRecord;
declare const abcObjectInterface: AbcObjectInterface;

// Expected return type to be `{ a: number; b: number; c: string }` and got :-)
// The second overload is used, as expected.
_.mapValues(abcObjectInterface, (v) => 1);

// Expected return type to be `Record<"a" | "b" | "c", number>` but got `{ [index: string]: number }` :-(
// The first overload is used, unexpectedly.
_.mapValues(abcObjectRecord, (v) => 1);

Related Issues: DefinitelyTyped/DefinitelyTyped#26983 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions