Skip to content

Feature request: Implicit typing of actions using TypeScript type guards #133

@ikorolev93

Description

@ikorolev93

If you have a function with a signature like (action: any) => action is FooAction in your type field, action should be FooAction in the hooks.
Proof of concept:

interface FooAction {
  type: "FOO";
  payload: {
    foo: number;
  }
}
type TypeGuard<A> = (action: any) => action is A;
declare const isFoo: TypeGuard<FooAction>;
interface LogicConfig<A> {
  type: TypeGuard<A>;
  process: (data: { action: A }) => void;
}
declare function createLogic<A>(cfg: LogicConfig<A>): void;
createLogic({
  type: isFoo,
  process({ action }) {
    action.payload.foo.toFixed(); // no error
    action.payload.bar; // error
  }
});

Unfortunately I'm not too familiar with this project's definitions to make a PR in the near future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions