-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
THIS IS NOT THE USUAL Object.keys TYPING "BUG".
TypeScript Version: 3.8.3
Search Terms: Object.keys
Code
function foo<T extends object>(bar: T): void {
for(const key of Object.keys(bar)) {
console.log(bar[key]); // TypeError
}
for(const key in bar) {
if(bar.hasOwnProperty(key)) {
console.log(bar[key]); // No error
}
}
}Issue
The two for-loops above should be functionally identical (with the exception that the second for-loop is less-efficient.) Accordingly, key should have the same type in each.
However, in the former, key is a string, per the logic given by @RyanCavanaugh on Stack Overflow; while, in the latter, key is Extract<keyof T, string>. Correct me if I'm understanding this wrong; but if keyof isn't safe in the former, it seems like it wouldn't be safe in the latter, either.
Related Issues:
#12253, #30314, #13254. #30228, #28899, #28284, #26901, #30749, #31087, #32321, #34498, #35145.
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug