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

Open
FumioNonaka opened this Issue Jan 5, 2017 · 2 comments

Projects

None yet

2 participants

@FumioNonaka

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.

@mhegazy
Contributor
mhegazy commented Jan 5, 2017

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;
}
@mhegazy
Contributor
mhegazy commented Jan 5, 2017

A PR would be welcomed.

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