-
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
Object.entries correct typings #35101
Comments
Duplicate of #20322. The issue boils down to: #12253 (comment) |
Hello @MartinJohns @fatcerberus and @RyanCavanaugh I am not convinced though, I thinks some assumptions can be challenged in regards of the current state of typescript. In particular, contravariance issues are widespread and don't always lead to Would you mind refreshing this topic and allowing me to advocate another opinion ? |
If you think there's some solution available, please do describe |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
So any further work on this? Okay, if object can hold more properties than const source: SomeType = ....;
const result: SomeType = Object.assign({}, ...Object.entries(source).map(([k, v]) => ({[k]: v}))); While it may process some extra properties that aren't part of Wouldn't it be nicer to have it like something about this?
So we don't know what elements of array are but we know for sure it has keys of T. I strongly believe that this code const obj = {A: 10};
const keys = Object.keys(obj);
if (keys.includes("A")) {
console.log("Hello");
} Should generate a warning "condition is always true". And it's true even if |
I've also found a great answer about how to restrict object from having extra properties. This may help as well: https://stackoverflow.com/questions/49580725/is-it-possible-to-restrict-typescript-object-to-contain-only-properties-defined |
i had to do smth like this:
and as you might guess key, value was not inferred properly so I did something like this:
and transform the first part to:
everything is typed OK now |
worked for me on TS 4.8.4!! |
Search Terms
Object.entries
Potentially related issues:
#32771
Suggestion
Typings of
Object.entries()
returned[key, value]
tupple array is very weak and infer poorly from argument, I think it would be better to implement more relevant ones in lib.es2017.object.d.tsUse Cases
When iterating on an object entries array returned by
Object.entries
, we need to manually type cast the array to avoid false positive and false negative typings shortcomings.It is not possible to infer typings in
.map
or.forEach
Examples
We can easily write our own
entries
function:Wich gives me an array of tupples with inferables key/value:
We may implement a better default behaviour for
Object.entries
in the lib:The same concepts can be applied for better typings of
Object.values
Checklist
My suggestion meets these guidelines:
I need some feedback to identify potential breaking changes.
I think it 'may' induce breaking change since it's reducing typecast necessity. If manual casts are incompatible with the current feature request, there may be a need to remove cast or force through an intermediary
any
.I tested it in a project of mine and had no issue since the existing cast was correctly made with the same source of truth as the
Object.values
argument type.I can't say I'm sure it does. Feedback would be appreciated.
The text was updated successfully, but these errors were encountered: