Skip to content

keyof does not work with extends in generic type argument. #16740

@We-St

Description

@We-St

TypeScript Version: 2.3.4

Code

function shallowCopy<T, U extends T>(s: T, d: U) {
  Object.keys(s).forEach((k: keyof T) => d[k] = s[k]);
}

Expected behavior:
I would expect this function to compile, since U is a sub class of T and U's keys are presumably treated as superset of T's keys. Each key of T should be a key of U as well.

Actual behavior:
TypeScript reports the following error: "Type 'T[keyof T]' is not assignable to type 'U[keyof T]'."

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