Jagrit Goyal
Jagrit Goyal

Reputation: 1

Ngrx props<>() method in createAction()

I wanted to ask that if we can give any default value in the props<>() just like we give a default value in function arguments

export const set = createAction(
    '[Counter] Set',
    props<{ value: number }>()
);

Here I want to give a default number to "value" property in props<{ value: number }>().

Is there any way of doing so?

I tried adding the number similar to what we do in function arguments:

export const set = createAction(
    '[Counter] Set',
    props<{ value = 0 }>()
);

But it resulted in error!:

Operator '<' cannot be applied to types '<P extends SafeProps, SafeProps = NotAllowedInPropsCheck<P>>() => ActionCreatorProps<P>' and '{ value: number; }'

Upvotes: 0

Views: 59

Answers (1)

Vivick
Vivick

Reputation: 4991

props<{ value: number }>() it's generics, not an embedded DSL.

You get that error because { value = 0 } is not a valid type. { value: 0 } is, but it means that value is always 0.

If you want to signify that there's a default value, use props<{ value?: number }>() or props<{ value?: number|0 }>().

Then in your reducer you can have something like:

export const myReducer = createReducer(
  initialState,
  // [...]
  on(set, (state, { value = 0 } = {}) => { /* [...] */ }),
  // [...]
)

Upvotes: 1

Related Questions