agiledigital / typed-redux-saga

An attempt to bring better TypeScript typing to redux-saga.
MIT License
314 stars 33 forks source link

Improve take type #11

Open danielnixon opened 4 years ago

danielnixon commented 4 years ago

Consider when the take effect is passed a string and an action type param, like this:

  type FooAction = {type: "FOO"};
  yield* Effects.take<FooAction>("FOO"); // $ExpectType FooAction

In cases like this we should be able to constrain the passed string to "FOO" | "*", giving some extra type safety.

See https://redux-saga.js.org/docs/api/#takepattern

danielnixon commented 4 years ago

@gilbsgilbs another one for you to ponder ☝️

danielnixon commented 4 years ago

If we ignore the function and array forms, something like this is what I'm doing (where MyAppActionType is the union of all my app's action types):

import { take as rawTake } from "typed-redux-saga";

export const take = <A extends MyAppActionType>(
  pattern: A["type"] | "*",
): SagaGenerator<"*" extends typeof pattern ? MyAppActionType : A, TakeEffect> =>
  rawTake(pattern);