-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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.values() appears to be unusable with custom types. #2133
Comments
Object type implies that an instance has particular properties, but it doesn't mean that it doesn't have any other properties. So, mixed is the only safe option |
@vkurchatkin well, it does if you annotate it:
I'd follow this issue: #2174 |
@vkurchatkin I've seen that touted a couple of times, but the main problem is that the same functionality can be implemented in a 'type-safe' way using Object.keys. type Person = { name: string };
type People = { [string]: Person };
const people = {
john: { name: 'john'},
frank: { name: 'frank' },
}
const fromValue: Array<Person> = Object.values(people); // fails
const fromKeys: Array<Person> = Object.keys(people).map(key => people[key]); // works Here is the same code in the 'Try' page. To my understanding, the |
Please reopen this issue. The last example posted by @deecewan demonstrates how |
given my understanding of how flow treats objects, I think this should be good as a libdef: declare function values<T>(p: { [string]: T }): Array<T>;
declare function values(p: Object): Array<mixed>; This way, if a known object is passed in (with only a single value type), it returns an array of the known values. If any other object type is passed in, it returns an array of mixed. this repl shows it in action. Unless I'm missing something, this should work. Edit: In fact, this same thing should work for |
Hmm, this looks promising. I'd like to examine this more closely. PR @deecewan? |
I started one earlier. There were a couple of issues. I'll finish it up and put it up for comment in the morning. Edit: #6385 |
@deecewan this looks correct |
It seems to work in most cases for |
Why is this still closed?
|
I just love it how there's now way to use |
@nnmrts I just avoid |
@deecewan Great! You could also just use Or even better, just use the No, wait, even better, just typecheck by hovering over every variable in your IDE, that will do it! Sorry but after over 3 years I'm expecting a little more than shitty workarounds for this, from a library developed by facebook. Whatever, sorry for ranting... |
I think other issue already exists |
Really, why is this issue closed? "Avoid using Object.values(), use Object.keys() instead" is not a solution. Code should not depend on instruments like Flow, especially when we talk about native methods like Object.values(). |
Why is this issue still closed? |
In lib/core.js:
This method doesn't seem to be compatible with a custom type because it is defined as a mixed array.
Simple example:
types:
function:
Which results in something like:
Does this mean flow users need to rewrite utility functions like .values() to account for custom types?
The text was updated successfully, but these errors were encountered: