Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createActionGroup doesn't handle discriminated union types correctly #3712

Closed
1 of 2 tasks
usarskyy opened this issue Dec 16, 2022 · 1 comment · Fixed by #3713
Closed
1 of 2 tasks

createActionGroup doesn't handle discriminated union types correctly #3712

usarskyy opened this issue Dec 16, 2022 · 1 comment · Fixed by #3713
Assignees

Comments

@usarskyy
Copy link

Which @ngrx/* package(s) are the source of the bug?

store

Minimal reproduction of the bug/regression with instructions

Playground: https://ngrx-seed-tyhkga.stackblitz.io

Code to reproduce the problem :

import { createAction, createActionGroup, props } from '@ngrx/store';

export const substracted = createAction(
  '[Counter] Value substracted',
  props<{ result: number, status: 'success' } | {status: 'error'}>()
);


export const unionTypesBugActions = createActionGroup(
  {
    source: 'Counter',
    events: {
      'value substracted': props<{ result: number, status: 'success' } | {status: 'error'}>(),
    }
  }
);


const oldStyleSubstractedAction1 = substracted({status: 'success', result: 0});
const oldStyleSubstractedAction2 = substracted({status: 'error'});

const groupStyleSubstractedAction1 = unionTypesBugActions.valueSubstracted({status: 'success', result: 0});
const groupStyleSubstractedAction2 = unionTypesBugActions.valueSubstracted({status: 'error'});

Error:

image

Minimal reproduction of the bug/regression with instructions

I expect that createActionGroup correctly handles discriminated union types, so TS compiler doesn't complain about conflicts.
Basically, the code below must compile successfully:

import { createAction, createActionGroup, props } from '@ngrx/store';

export const substracted = createAction(
  '[Counter] Value substracted',
  props<{ result: number, status: 'success' } | {status: 'error'}>()
);


export const unionTypesBugActions = createActionGroup(
  {
    source: 'Counter',
    events: {
      'value substracted': props<{ result: number, status: 'success' } | {status: 'error'}>(),
    }
  }
);


const oldStyleSubstractedAction1 = substracted({status: 'success', result: 0});
const oldStyleSubstractedAction2 = substracted({status: 'error'});

const groupStyleSubstractedAction1 = unionTypesBugActions.valueSubstracted({status: 'success', result: 0});
const groupStyleSubstractedAction2 = unionTypesBugActions.valueSubstracted({status: 'error'});

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

NgRx: 14.3.2
Angular: 14.2.6
Node: 16

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@markostanimirovic
Copy link
Member

markostanimirovic commented Dec 16, 2022

Good catch @usarskyy!

For now, you can use a props factory for union types:

export const demoActions = createActionGroup({
    source: 'Demo',
    events: {
      'Demo Action': (props: { result: number, status: 'success' } | { status: 'error' }) => props,
    },
});

// works as intended
demoActions.demoAction({ result: 1, status: 'success' });
demoActions.demoAction({ status: 'error' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants