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

for..in variable does not have the type keyof T #12314

Closed
mhegazy opened this issue Nov 16, 2016 · 1 comment
Closed

for..in variable does not have the type keyof T #12314

mhegazy opened this issue Nov 16, 2016 · 1 comment
Assignees
Labels
Fixed A PR has been merged for this issue

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Nov 16, 2016

function set<T, K extends keyof T>(obj: T, key: K, value: T[K]) {
    obj[key] = value;
}

var O = { a: "string" };

// var k: keyof typeof O;
for (var k in O) {
    set(O, k, "another"); // string is not assignable to "a"
}
@ahejlsberg
Copy link
Member

I have my doubts about this one. In for (var k in x) where x is of some type T, it is only safe to say that k is of type keyof T when the exact type of x is T. If the actual type of x is a subtype of T, as is permitted by our assignment compatibility rules, you will see values in k that are not of type keyof T.

The same argument holds for the built-in Object.keys() method.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants