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

idx() callbacks may only access properties on the callback parameter! #4729

Closed
xareelee opened this issue Aug 28, 2017 · 2 comments
Closed

Comments

@xareelee
Copy link

xareelee commented Aug 28, 2017

The following code raises a flowtype error when using idx with immutable.js:

// `state` is obtained from redux store
// `state.user` is an immutable object; you need to use `getIn()` to access properties
const token = idx(state, _ => _.user.getIn(['userInfo', 'token']));

The flowtype shows the error:

 81:   const countryCode = idx(state, _ => _.country.getIn(['selectCountry', 'countryCode']));
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function call. idx() callbacks may only access properties on the callback parameter!

I need to change code to this to suppress flowtype warnings:

const tokenS = idx(state, _ => _.user.getIn);
const token = (tokenS) ? tokenS(['userInfo', 'token']) : undefined;

It makes no sense to write such code.

@asolove
Copy link
Contributor

asolove commented Aug 28, 2017

Is there a reason why you want to use Immutable's getIn inside of idx? The example code here could just be replaced with user.getIn(["userInfo", "token"]), which is already safe in the case where those nested keys might not exist.

I suppose you might want to use the two together in a case where some of the nested objects were plain JS objects?

In any event, the machinery that idx uses to make the nested lookup safe only works when accessing nested properties. It doesn't work when accessing inherited methods like getIn. So I don't think your request to make this work is going to happen. But if you can tell me more about the specific situation you're in, I bet we can find a creative solution that solves it.

@rocketraman
Copy link

Doing a null-safe chained access with functional methods is not an unusual thing. For example, given an object err that possibly contains a property graphQLErrors which is an array, one value of which possibly contains a property userMessage, we want to extract the first userMessage. One would think one could do:

let msg = idx(err, _ => _.graphQLErrors.find(e => e.userMessage).userMessage)

In languages that support the null-safe operator, like Kotlin or C# this would translate exactly to:

err.graphQLErrors?.find(e => e.userMessage)?.userMessage

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 20, 2023
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

4 participants