-
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
Incorrect mixed type from value of Object.entries #5838
Comments
Problem is here: https://github.com/facebook/flow/blob/master/lib/core.js#L48 |
See #2221. I'm currently using the values/entries code in the bottom of this file to great effect. https://github.com/chili-epfl/FROG/blob/develop/frog-utils/src/index.js#L285-L293 |
Would replacing the current declaration linked to by @spudly with this work? Anyone care to test it out and submit a PR? |
No @mrkev, the definitions for values and entries that @houshuang linked to are unsound. Here is a simple example: const entries = <T>(obj: { [string]: T }): Array<[string, T]> => {
const keys: string[] = Object.keys(obj);
return keys.map(key => [key, obj[key]]);
};
const values = <T>(obj: { [string]: T }): Array<T> => {
const keys: string[] = Object.keys(obj);
return keys.map(key => obj[key]);
};
function test(val: { foo: string }) {
values(val).forEach(v => {
v.toLowerCase();
});
entries(val).forEach(([ k, v ]) => {
v.toLowerCase();
});
}
test({ foo: 'x', bar: 123 }); This going to pass typechecking, but fail at runtime. |
Indeed. In our case, we only use it for well typed dictionary objects (of
the type [id: string]: WellTypedObject) so we always know what to expect.
…On Mon, Jun 4, 2018 at 6:09 PM, James Wyatt Cready-Pyle < ***@***.***> wrote:
No @mrkev <https://github.com/mrkev>, the definitions for values and
entries that @houshuang <https://github.com/houshuang> linked to are
unsound. Here is a simple example:
const entries = <T>(obj: { [string]: T }): Array<[string, T]> => {
const keys: string[] = Object.keys(obj);
return keys.map(key => [key, obj[key]]);
};
const values = <T>(obj: { [string]: T }): Array<T> => {
const keys: string[] = Object.keys(obj);
return keys.map(key => obj[key]);
};
function test(val: { foo: string }) {
values(val).forEach(v => {
v.toLowerCase();
});
entries(val).forEach(([ k, v ]) => {
v.toLowerCase();
});
}
test({ foo: 'x', bar: 123 });
This going to pass typechecking, but fail at runtime.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5838 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AADwh5qf6t2LDPS_exJufrRGjpSVnythks5t5VuegaJpZM4SJ6wA>
.
--
http://reganmian.net/blog -- Random Stuff that Matters
|
@jcready ah true, inexact objects back at it again |
…ictionary (mixed otherwise) Summary: User request from Dec 2021, May 2021, Dec 2020, Apr 2020 For `Object.entries`, keys is typed exactly like it is for `Object.keys` For both, values is typed as such: - Dictionaries: the value of the dictionary - Objects & Instances: `mixed` - this avoids created non-performant union types Error diff analysis: - Previously, `Object.entries` returned `string` for the key type, rather than having the same behavior as `Object.keys`. We unsafely allow property read/writes with `string`. - Error code for suppressions has changed in some places from `incompatible-call` to `incompatible-use` - Many errors have simply moved (previously error on usage of `mixed`, now erroring on still invalid but more accurate type) After WWW diff D41527152, errors are only around ~500. Alternative 1 was to create a new utility type `$DictValues` and type the `Object.values` and `Object.entries` lib defs with this. Cons of this approach are introducing a new lib def, and the fact that `Object.entries` keys definition would differ from `Object.keys`, since `$Keys` and `Object.keys` are implemented differently. Changelog: [errors] Instead of `mixed`, type the result of `Object.values` and `Object.entries` on a dictionary to be the dictionary values, and `Object.entries` keys to behave like `Object.keys` Closes #2174, #2221, #4771, #4997, #5838 Reviewed By: jbrown215 Differential Revision: D35710131 fbshipit-source-id: 3163bc02dfc7cb70a5d6c0ba7da574a793672421
With above commit, object types that have an indexer and no explicit properties (e.g. |
Flow assumes that
value
as returned by[key, value]
for Object.entries is mixed. Not really sure where to start looking but willing to check it out / try to help!Error:
Try Flow
The text was updated successfully, but these errors were encountered: