Skip to content

Mapped types does not work with generic type arguments extending an object #35647

Closed
@HosseinAgha

Description

@HosseinAgha

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms: mapped types generics bug

Code

I can confirm that this does not work:

  const genericFunc = async <Schema extends { _id: string }>() => {
    type a = { [K in keyof Schema]: Schema[K] };
    const b: a = { _id: 'sd' };
  }

And this does work:

  type Schema2 = { _id: string };
  type a2 = { [K in keyof Schema2]: Schema2[K] };
  const b2: a2 = { _id: 'sd' };

Expected behavior:
IMO TypeScript should assume generic parameter above is something like this:

interface Schema {
  _id: string;
  [K: string]: any;
}

and does not throw errors.

Playground Link: example link
Related Issues: I'm one of the maintainers of the @types/mongodb in definitely typed. This is the original issue DefinitelyTyped/DefinitelyTyped#39358

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions