Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

The example code in "Generic" does not run #475

Description

The example code of "Using Type Parameters in Generic Constraints" in "Generic" does not run. The following error is shown.

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

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 });

In fact, this does not run in the Playground.

Activity

  1. mhegazy commented on Jan 5, 2017

    @mhegazy
    Contributor

    This example is not correct now. It should be typed as:

    function copyFields<T, K extends keyof T>(target: T, source: Pick<T, K>): T {
    	for (let id in source) {
    		target[id] = source[id];
    	}
    	return target;
    }
  2. mhegazy commented on Jan 5, 2017

    @mhegazy
    Contributor

    A PR would be welcomed.

  3. Swap76 commented on Oct 3, 2019

    @Swap76
    Contributor

    Hey I am newbie will love to submit a PR

  4. sasurau4 commented on Oct 22, 2019

    @sasurau4

    This issue seems fixed by #489

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions