-
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
Is there a way to correctly type check this? #673
Comments
Your first example is confusing me, so I'm ignoring it ( With respect to the second, I expect that disjoint union types make short work of this, as long as you're okay with Flow inferring the function types. Just provide a type Action = "action" | "anotherAction" | ... This smells a lot like a pattern heavily used in Flux, so I would search for Flow-typed Flux stuff for analogous code. |
@popham in the first example, As for the second one, inferring is not working, and I think it's due to, AFAIK, the fact that it's in a separate module and Flow needs you to have types in the modules boundaries. It actually is sort-of Flux code, I want to solve this problem in this effort to provide Flow types for the redux library: reduxjs/redux#356 |
@leoasis lets focus on the second one, and the solution should extrapolate. Have you tested the distinction? That is, have you tried this with everything in a single module? |
I've just tried and it seems to be working correctly. This is the real implementation of that function I'm talking about: https://github.com/leoasis/redux/blob/bec3cb222f844f148bd0ed671629ae218ba7459f/src/utils/combineReducers.js As you see, it has a lot of dependencies as well which makes the type inference no longer work. I've replaced that function in my example with this one, which does more or less the same: function combineReducers(reducers) {
return function combination(state = {}, action) {
return Object.keys(reducers).reduce((fullState, key) => {
var reducer = reducers[key];
var newState = reducer(state[key], action);
fullState[key] = newState;
return fullState;
}, {});
};
} And it correctly type checks, but only in the same file. Is there a way to achieve that behavior across modules? Or, is there a type signature for this function that provides the same behavior? |
By "working correctly", I'm assuming that you mean Flow processes your module with 0 errors. I'm also assuming that in the imports case you're importing a modules that contains the above function as a default, I think that it's safe to declare this a bug in Flow's import/export mechanics.
|
(I just noticed that your file doesn't have a |
@popham First of all, thanks a lot for your support on this issue (haven't thanked you before :)). Now continuing... By "working correctly" I mean that it throws 0 errors when the types are correct, and throws type errors when they're not. Will try what you mentioned about not using the default export, and get back here. Regarding your second comment about the Anyway, I'd really like to know if there's a type signature that will make that code behave as if it was inferred, so at least the type is documented. Thanks again for your help! |
Doh. I forgot about the module boundary limitation. All exported stuff needs to be annotated. I think you're stuck with
I'm having trouble even imagining a tidy syntax that could support this. I would suggest rearchitecting the code to contain the state machine within a class, but I'm betting that runs contrary to your goals with respect to that library. |
@popham yeah I was trying to see if this is even possible in type systems such as in Haskell, and if not, what's the alternative for an equivalent function or something with similar behavior. Will try your approach with |
This has been partially resolved with utility types such as $ObjMap. Researching more about that. |
I'm trying to figure out how to correctly type functions that accept an object literal as a record of functions and return another record with the same properties but values that have a type that depends on the type from the parameter's property.
Some concrete examples:
It seems kind of related to this issue, I think: #362.
Not sure how this is actually solved in other type systems, or if it is even possible to do.
Any help or guidance regarding how to type this, or to know if it is even possible to type this, would be awesome!
The text was updated successfully, but these errors were encountered: