-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Object.entries
assumes the key is always a string
#51572
Comments
This is intentional. Objects are not sealed. See #38520 for more information. |
Well, I think the use case I stated here is a bit different than being based dynamically on keys. Instead of using string as the key type, there's a union type restricting the key type to a stricter one. The fix I suggested is probably bad though :D but I don't see how this relates to the Again, I probably miss something here and probably lack of understanding the underlying issue. |
You're assuming the keys can only be |
This is where I fail to understand what additional keys could possibly be there. As per the MDN docs for Object.entries() it seems only the actual keys are created. Is there a real life example of such divergence in the object/entries keys that could help me get this? Anyway, is there a way aside from casting with I'll read the whole thread again later on to make sure I didn't miss any crucial piece of explanation. Thank you for trying to explain this to me. |
Here's a good example: #12253 (comment) TypeScript uses structural typing, not exact types. So extra keys may be allowed on an object, outside its type. |
On the subject of exact types: #12936 |
For what it's worth, I use these custom wrappers: export const entries = Object.entries as <T>(
obj: T
) => Array<[keyof T, T[keyof T]]>
export const keys = Object.keys as <T>(obj: T) => Array<keyof T>
export const values = Object.values as <T>(obj: T) => Array<T[keyof T]> Here's a playground. |
@nandorojo These wrappers are fairly common. Personally, I'd add the prefix |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
just do this and the problem is gone
I added // @ts-ignore because ts would tell me this:
If someone have a solution to get rid of // @ts-ignore without loosing the ability to preserve the dynamic aspect of T, let us know in the comments If this breaks your code you can do:
|
I don't have such issue, but you may try to use Also your first example produces union of values from every key which gives poor type narrowing even when explicitly checking the property: You should follow the pattern above and use indexed access type instead of manually assigning union |
lib Update Request
Configuration Check
My compilation target is
ESNEXT
and my lib isthe default
.Missing / Incorrect Definition
Object.entries()
Sample Code
Fix
Possible fix would be to update the definition in lib.es2017.object.d.ts like this:
The text was updated successfully, but these errors were encountered: