Skip to content
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

Closed
EvHaus opened this issue May 31, 2018 · 4 comments

Comments

@EvHaus
Copy link

EvHaus commented May 31, 2018

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 an Array, or extend the $Values<> type to support Arrays instead of only Objects.

The specific use case I'm after is something like this:

const list = ['a', 'b', 'c'];

const myFunc = function (key: $Values<typeof list>) {
   console.log(key);
}

// This should be allowed
myFunc('a');
myFunc('b');
myFunc('c');

// This should throw an error because the given argument isn't one of the elements in my Array
myFunc('d');
myFunc('e');
myFunc(0);
@EvHaus 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
@Mr-Wallet
Copy link

This may be a duplicate of #961

@gkz gkz closed this as completed Apr 10, 2020
@gkz gkz reopened this Apr 10, 2020
@jcready
Copy link
Contributor

jcready commented Apr 13, 2020

This also works:

type ArrayValues<TArr> = $ElementType<TArr, number>;

@gkz gkz closed this as completed Apr 13, 2020
@EvHaus
Copy link
Author

EvHaus commented Apr 13, 2020

@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

https://flow.org/try/#0C4TwDgpgBAggTnAhiAaogNgVwgZwDwAq8cAfFALxQAkAwhunoSQBTHJMCUFZBANFEQQkA3ACgA9OKihIsBMjRZchYmUpUAouggBbCADtgBcBBUJ++zDoBGEUmNEBjAPb6cwKOgCW7ilADaAOSIgfyB1qFQgY6BALoOLm4eOiAAYpj6jn4AZhmOwF6uUMwA1hAgAFxySKgY2PgyEM7Znj7AJFwA3qJQvYk4ztoAdOjOAOal5RxiAL6iElIEABY+UDhLzpjoACZQtlD0zgDuENuiKemZzMGB0+dpedcRdxeP0bcOkgIrOGsbW7tgEs4McDvooHYQXA9hBHIhMDhoEDoGMvAA3AwHOBjKwGDw+fSBDyuaDNaRLaAQbR6Qy-LzglLVZD3S6Oa7bD4st4QTlAA

It seems to work on types number vs string but doesn't interpret string enums like Flow does with object keys:

// 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
Projects
None yet
Development

No branches or pull requests

5 participants