-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve Object.keys(object) and Object.values(object) #26901
Comments
Duplicate of this closed PR #25832. As an example why: const object = {
abc: 123 as 123,
def: 456 as 456
}
const objectKeys = Object.keys<{abc: 123}, 'abc'>(object) // objectKeys: Array<'abc'> whoops, array will contain 'def' |
const FooKeys = Object.keys(Foo) // FooKeys: Array<'abc' | 'def'> keys of enum has not only "key" |
While crawling through the issues I found this merged PR: #18175 |
@AlCalzone I think there is simply no way to represent this safely. Subtyping and this kind of reflection are incompatible because there is no correspondence between the value and the typescript type. The reason |
Dates back to at least #12253 |
Search Terms
Suggestion
When all keys and values of
object
is knownObject.keys(object)
should return array type of union of known propertiesObject.values(object)
should return array type of union of known valuesHow to implement?
I tried creating the 2 functions:
I also tested them: playground link
Use Cases
Use with objects
Use with enums
Caveats
It is possible to ignore extra keys/values that don't exist in type definition
Examples
See use cases and playground link above
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: