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

Request: Add support for changing a Array<?type> to a Array<type> via 'filter' #2575

Closed
oreqizer opened this issue Oct 4, 2016 · 3 comments

Comments

@oreqizer
Copy link

oreqizer commented Oct 4, 2016

Let's say I have the following:

const maybeGet = (param: string): ?Object => param === "correct" ? { data: "data" } : null;

I want to fetch a bunch of those, that will give me Array<?Object>, then filter out those that don't match. That should produce Array<Object>. Say I want to do this:

function fetchStuff(params: Array<string>): Array<Object> {  // NOPE, Flow shouts at me
  const maybeObjs: Array<?Object> = params.map(maybeGet);

  return maybeObjs.filter(id => id);  // all nulls will get removed
}

Would it be possible to permit something like this? Maybe some other neat way of replicating this behavior?

Note:
I managed to do this via reduce:

function fetchStuff(params: Array<string>): Array<Object> {  // OK
  const maybeObjs: Array<?Object> = params.map(maybeGet);

  return maybeObjs.reduce((arr, obj) => {
    return obj ? arr.concat(obj) : arr;
  }, []);
}

but it's not really nice to do this all the time, when the filter verison is so much simpler.

@vkurchatkin
Copy link
Contributor

This is possible already:

const arr: Array<?{}> = [null, {}, {}, null];
const arr1: Array<{}> = arr.filter(Boolean);

@gabro
Copy link
Contributor

gabro commented Oct 4, 2016

Isn't this the same topic as #1414?

@oreqizer
Copy link
Author

oreqizer commented Oct 4, 2016

ohhh thanks @vkurchatkin didn't try it that way. been using the id => id thing

@oreqizer oreqizer closed this as completed Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants