-
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
Another Object.{entries,values} attempt #6385
Conversation
hahha I gochu-- gonna fufill Cunningham's law here (didn't know it was a thing, but I guess it kinda is true a lot of times?). The issue is width subtyping, which is necessary and shouldn't be deemed incorrect, but makes things a little tricky. Ergo, take this example:
This typechecks, and with this definition of
|
Thanks for the explanation @mrkev. Width subtyping is definitely necessary, and I hadn't considered it here. |
beating a dead horse here, but if there was a way to say 'if the type is an exact type, use this libdef', we could to this, right? the would prevent the width subtyping issue and let people who were using exact types use a stricter, more helpful definition of pseudocode: declare function values<T>(object: $IsExactType<{ [any]: T }>): Array<T>;
declare function values(object: Object): Array<mixed>;
type T = {a: string}
type C = {|a: string|}
function f (obj: T) {
return values(obj)
}
function fc(obj: C) {
return values(obj);
}
const foo: Array<T> = f({a: "hello", b: 4}); // fails, because T is not an exact type
const bar: Array<C> = fc({a: "hello"}); // passes because C is an exact type. |
@deecewan there is a way, but it isn't correct, technically exact types doesn't have supertype |
Why does that example type check without errors though? I would have expected a type error on the call to f(obj) - since obj is not type T. |
I've read through a whole load of posts surrounding this topic, but can't quite get my head around exactly why
Object.keys
works in every case, butObject.entries
andObject.values
are unsafe.Most importantly, why
Object.keys(object).map(k => object[k])
can correctly (and safely, apparently) pull out the values from a{ [string]: T }
, but we can't have the same happen fromObject.values
.So I've opened a PR, because then Cunningham's Law says I'll get an answer (or maybe this is correct).
I've added tests, but I'm almost certain that these don't cover every case.
I've split this into two commits so that the updates to the test files don't get in the way of the code I've added.