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

Explicitly returning null in .map() #12

Open
benadamstyles opened this issue Jun 20, 2017 · 7 comments
Open

Explicitly returning null in .map() #12

benadamstyles opened this issue Jun 20, 2017 · 7 comments

Comments

@benadamstyles
Copy link
Contributor

I am using maybe in React Native, in the props of a custom text component. The custom component should set the underlying Text component prop numberOfLines to 1 by default, or null if the user passes false into the custom component's truncate prop:

<BasicText
  numberOfLines={maybe(props.truncate)
    .filter(t => !t)
    .map(() => null)
    .orJust(1)}
/>

However, flow understandably complains about returning null in .map():

null (This type is incompatible with union: number | boolean | string | object type | array type
See also: call of method `map`

Is there an "idiomatic" way to handle this, or is it simply a case of putting an // $ExpectError comment and forgetting about it?

@alexanderjarvis
Copy link
Owner

Why would truncate false set numberOfLines to null? Should it be the other way around?

@benadamstyles
Copy link
Contributor Author

Thanks for getting back so quickly. I probably should have used a less convoluted example! truncate: false should set it to null because null is the only way I know of making a react prop "have no effect". When numberOfLines is set to null, it doesn't do any truncating. If it's set to 1, then ellipsizeMode comes into force.

But basically what I mean is: there are some situations where we might need to return null. Is there a good way to do that?

@benadamstyles
Copy link
Contributor Author

Put another way: we are allowed to do .orJust(null) My use case above is really just the inverse of that, but the flowtyping won't allow it.

@alexanderjarvis
Copy link
Owner

alexanderjarvis commented Jun 20, 2017 via email

@benadamstyles
Copy link
Contributor Author

Ok, I guess I can't use maybe for this. Just to be clear though, I can't use flatmap and return nothing because that would trigger the .orJust(1) whereas I explicitly want to return null. I'm not asking maybe to implicitly convert null to nothing.

I guess treating null as a value in this way goes directly against the concept of this lib. I don't suppose there would be a way to wrap it or something to appease flow?

@alexanderjarvis
Copy link
Owner

alexanderjarvis commented Jun 20, 2017

Would this work?

maybe(props.truncate).filter(t => t).map(t => 1).orJust()

I think you can return undefined for numberOfLines.

This lets you:

  1. optionally provide truncate
  2. if truncate is true then it returns 1
  3. if truncate is missing or false, it returns undefined

The vanilla equivalent is:

props.truncate && 1

which to be honest, is much simpler, if you're ok with undefined.

@benadamstyles
Copy link
Contributor Author

The problem is the default should be 1, not null or undefined. The vanilla equivalent is:

props.truncate == null ? 1 : (props.truncate ? 1 : null)

...which is why I wanted to use maybe!

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

2 participants