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 98 forks source link

Type inference issue for "createAction" API #47

Closed piotrwitek closed 6 years ago

piotrwitek commented 6 years ago

Added missing test scenario for createAction when payload is undefined and only meta should be returned. Test case:

it('with meta', () => {
    const action = createAction(types.WITH_META, resolve => {
      return (token: string) => resolve(undefined, token);
    });
    const actual: { type: 'WITH_META'; meta: string } = action('token'); // Error:   Property 'meta' is missing in type '{ type: "WITH_META"; }'.

    expect(actual).toEqual({ type: 'WITH_META', meta: 'token' });
  });
piotrwitek commented 6 years ago

Fixed bug in createAction and added consistent behaviour throughout the entire API surface: From now on you can do:

const action = (meta: string) => action(undefined, meta);
action('token'); // { type: 'WITH_META', meta: 'token' }

const action2 = createAction('WITH_META', resolve => {
  return (meta: string) => resolve(undefined, meta);
});
action2('token'); // { type: 'WITH_META', meta: 'token' }

const action3 = createStandardAction('WITH_META').map(
  (meta: string) => ({ meta })
);
action3('token'); // { type: 'WITH_META', meta: 'token' }

const action4 = createStandardAction('WITH_META')<void, string>();
action4(undefined, 'token'); // { type: 'WITH_META', meta: 'token' }