Skip to content

[TS] Type safety is not good enough when using $ngRedux.connect() #207

Open
@arutkowski00

Description

@arutkowski00

Hello,
let me present you some code to start with:

import { INgRedux } from 'ng-redux';

import { RootState } from '../';

import * as actions from './actions';
import * as selectors from './selectors';

export class FooController implements ng.IController {
  readonly bar: ReturnType<typeof selectors.getBar>;
  readonly fooBar: ReturnType<typeof selectors.getFooBar>;

  readonly load: typeof actions.load;
  readonly save: typeof actions.save;

  /* @ngInject */
  constructor(private $ngRedux: INgRedux) {
    $ngRedux.connect((state: RootState) => ({
      bar: selectors.getBar(state),
      fooBar: selectors.getFooBar(state),
    }), actions)(this);
  }
}

As you can clearly see, I have to repeat declarations twice: once, in $ngRedux.connect() and second, in the controller to be type-safe. If, for some reason, I start refactoring this code, by changing names of properties in connect() or changing the action creators' names imported in ./actions, this code will not throw any compilation error.

Question: is there a way to improve type-safety with $ngRedux.connect()? Maybe connect() should limit properties/actions in map***ToTarget to only those that are declared in the controller?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions