Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
We-St opened this issue Jun 26, 2017 · 3 comments
Closed

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

We-St opened this issue Jun 26, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@We-St
Copy link

We-St commented Jun 26, 2017

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]'."

@ghost
Copy link

ghost commented Jun 26, 2017

Actually, this seems to be preventing a genuine bug.

interface Tee {
	x: number | string;
}

interface Yoo extends Tee {
	x: number;
}

let t: Tee = { x: "" };
let u: Yoo = { x: 1 };
t = u;
u = t; // Error

shallowCopy<Tee, Yoo>(t, u);

function shallowCopy<T, U extends T>(t: T, u: U) {
	const tKeys = Object.keys(t) as Array<keyof T>;
	for (const key of tKeys) {
		let uk = u[key];
		let tk = t[key];
		tk = uk; // OK -- `number | string` = `number`
		uk = tk; // Error -- `number` = `number | string`
	}
}

The funny thing is that if you had just done function shallowCopy<T>(t: T, u: T) we would have allowed you to call shallowCopy<Tee>(t, u);, even though that is an error too! That's because we normally treat properties as readonly when considering assignability, even though they aren't in this case. I think #13347 is tracking that sort of issue.

@ahejlsberg
Copy link
Member

As @andy-ms is pointing out, this is working as intended. You are right that each key of T is a key of U as well, but a U[K] may be a subtype of the corresponding T[K] for the same K, and T[K] is therefore not assignable to U[K].

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 27, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants