Skip to content

'Using Type Parameters in Generic Constraints' code error #13651

Closed

Description

@leebenson

TypeScript Version: 2.1.5

Code

Taken from 'Using Type Parameters in Generic Constraints' in the official docs

function copyFields<T extends U, U>(target: T, source: U): T {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}

let x = { a: 1, b: 2, c: 3, d: 4 };
copyFields(x, { b: 10, d: 20 });

Expected behavior:

Compiles.

Actual behavior:

error TS2322: Type 'U[keyof U]' is not assignable to type 'T[keyof U]'.
Type 'U' is not assignable to type 'T'.

Activity

  1. gcnew commented on Jan 24, 2017

    @gcnew
    Contributor

    I raised the documentation issue in #13023

  2. added a commit that references this issue on Jan 24, 2017
  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThe issue relates to how you learn TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions