-
Couldn't load subscription status.
- Fork 13.1k
Open
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
Object.entries Object.values
π Version & Regression Information
This has existed since Typescript 3.9.7, which is when definitions for Object.entries/values were added.
β― Playground Link
π» Code
const MyObj = {
expectedkey: "hi"
} as const
type MyObjType = typeof MyObj
function OnlyMyObj(obj: MyObjType){
// val is improperly(??) narrowed down to "hi"
for (const [key, val] of Object.entries(obj)){
console.log(key, val)
}
for (const val of Object.values(obj)){
console.log(val)
}
}
function test() {
const more = {
expectedkey: "hi",
unexpectedkey: "boo"
} as const
OnlyMyObj(MyObj)
OnlyMyObj(more)
}
test()π Actual behavior
The type for val in OnlyMyObj() is incorrectly narrowed to "hi" when it's entirely possible to get other values if there are unexpected keys.
π Expected behavior
Since the key from Object.entries/keys is currently not narrowed to account for cases of there being extraneous keys, I believe the proper type for val would be unknown.
Additional information about the issue
Typescript already catches a very similar error and has the error at the call site which would be preferable to changing the return type of entries/values. Not sure if that can be done though.
const MyObj = {
expectedkey: "hi"
} as const
type MyObjType = typeof MyObj
function OnlyMyObj(obj: Record<string,"hi">){
// val is improperly(??) narrowed down to "hi"
for (const [key, val] of Object.entries(obj)){
console.log(key, val)
}
for (const val of Object.values(obj)){
console.log(val)
}
}
function test() {
const more = {
expectedkey: "hi",
unexpectedkey: 9
} as const
OnlyMyObj(MyObj)
OnlyMyObj(more)
}
test()Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created