piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 99 forks source link

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

Closed hsz closed 4 years ago

hsz commented 4 years ago

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?

piotrwitek commented 4 years ago

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

hsz commented 4 years ago

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