-
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
Feature Request: Utility function to get the values of an array (similar $Values for Objects) #6402
Labels
Comments
EvHaus
changed the title
Feature Request: Utility function to get the values of an array (similar $Keys for objects)
Feature Request: Utility function to get the values of an array (similar $Values for Objects)
May 31, 2018
This may be a duplicate of #961 |
|
This also works: type ArrayValues<TArr> = $ElementType<TArr, number>; |
@gkz @jcready Unfortunately neither of those work for my original use case type ArrayValues<TArr> = $Call<<T>(Array<T>) => T, TArr>;
// type ArrayValues<TArr> = $ElementType<TArr, number>;
const list = ['a', 'b', 'c'];
const myFunc = function (key: ArrayValues<typeof list>) {
console.log(key);
}
myFunc('a');
myFunc('b');
myFunc('c');
myFunc('d'); // This should fail but doesn't
myFunc('e'); // This should fail but doesn't
myFunc(0); // This fails correctly It seems to work on types // This works great!
const list = {'a': true, 'b': true, 'c': true};
const myFunc = function (key: $Keys<typeof list>) {
console.log(key);
}
// This is allowed
myFunc('a');
myFunc('b');
myFunc('c');
// This is not allowed
myFunc('d');
myFunc('e');
myFunc(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the moment, flow provides a useful
$Values<>
type for getting the type of the keys of an Object. Could we also get a utility that gives us the values of anArray
, or extend the$Values<>
type to support Arrays instead of only Objects.The specific use case I'm after is something like this:
The text was updated successfully, but these errors were encountered: