piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture

Home Page:https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

createAction's 4th type is supposed to be an array - is it really required?

hsz opened this issue · comments

After the latest release, there is createAction available with the following typing:

createAction<
  TType extends TypeConstant,
  TCreatorPayload extends any = undefined,
  TCreatorMeta extends any = undefined,
  TArgs extends any[] = any[]
>

So now if I'd like to specify all of the types, I need to:

const myAction = createAction<TypeConstant, { foo: string }, { bar: string }, string[]>(
  'MY_ACTION',
  arg => ({ foo: arg }),
  arg => ({ bar: arg }),
)();

myAction('hi!');

So - to define the arg type (and myAction arguments as well) as string I need to provide string[].
Is it really necessary? It looks like a fake requirement, because it can be handled like:

export function createAction<
  TType extends TypeConstant,
  TCreatorPayload extends any = undefined,
  TCreatorMeta extends any = undefined,
  TArgs extends any = undefined
>(
  type: TType,
  payloadCreator: undefined | ((...args: TArgs[]) => TCreatorPayload),
  metaCreator?: (...args: TArgs[]) => TCreatorMeta
): <
  TPayload extends TCreatorPayload = TCreatorPayload,
  TMeta extends TCreatorMeta = TCreatorMeta
>() => (...args: TArgs[]) => ActionBuilder<TType, TPayload, TMeta>;

What do you think?

Hey @hsz,
It is not a false requirement its purpose is to get an inference in case of payload/meta creators with multiple parameters, here is a comparison before and after your change:
BEFORE:
Screen Shot 2019-11-08 at 10 53 29 PM
AFTER:
Screen Shot 2019-11-08 at 10 55 34 PM

That makes sense. Thank you for the explanation and delivering the v5!