Skip to content

Is there a way to correctly type check this? #673

Closed
@leoasis

Description

@leoasis

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:

// Takes an object as a record with values that are functions with different arguments each (possibly), and a function to bind, and returns an object with the same properties, with those functions that were values bound to call that second parameter function
function bindActionCreators(actionCreators, dispatch) {
  return mapValues(actionCreators, actionCreator => {
    return (...args) => dispatch(actionCreator(...args));
  });
}
// Accepts an object with properties whose values are functions of type: (State, Action) => State (in this example, Action type is the same among reducers, but State can be of different type
// and returns a reducer function of type (CombinedState, Action) => CombinedState, where CombinedState is an object whose properties are the same as the object passed to `combinedReducers`, and the values are the accumulated values of each reducer function.
combineReducers(reducerFn) => reducerFn

// Example
function foo(state: number, action: Action): number {...}
function bar(state: string, action: Action): string {...}
var reducer = combineReducers({foo, bar})

var initalState = {foo: 0, bar: ''};
reducer(initialState, {some: 'action'}); //=> {foo: 3, bar: 'whatever'}

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions