Skip to content

Use optional type instead of dynamic for actions #77

Open
@hacker1024

Description

@hacker1024

Proposal

It'd be great if Store had another generic type to be used as the action argument type in methods like dispatch, preventing non-action objects from being used as actions.

This should be non-breaking for most users, as type inference should allow Store constructors to omit the existing state type, and the addition of another type will only break invocations that specify it explicitly.

Reasoning

I've separated my project into layers, like so:

Data:
project_data: Platform-agnostic repository implementations

Domain:
project_domain: Core platform-agnostic functionality, including state management

Platform:
project_flutter: A GUI using Flutter
project_cli: A CLI

Platform implementations can build a Redux store with a buildStore function in the domain package, passing in repository implementations to use. The domain package exports different action classes that the platform implementation can dispatch to the store. Each action class extends from an internal (non-exported) AppAction class.

If the dispatch function only accepts AppAction subtypes, this will prevent non-action objects being used by the platform implementations, requiring all actions to be used from the domain package.

Workarounds

At the moment, an assert can be used, but this throws an error at runtime. A compilation error is preferred.

AppState appReducer(AppState state, dynamic action) {
  assert(action is AppAction,
      'The action object ($action) has an invalid type (${action.runtimeType}! It must extend AppAction.');

  // ...
}

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