Anna Lee
Anna Lee

Reputation: 951

Action does not have payload in @ngrx/store

I have Angular application developed in the Angular 2 and ngrx/store1 or 2 (I do not have package.json or node_moduels). I am trying to set up that application in the Angular 5 and ngrx/store 5.1.0. As expected, there are something not available in recent version. One crucial this is not working is that Action does not have payload. If I instantiate Action with payload, it returns, "error TS2339: Property 'payload' does not exist on type 'Action';" I followed Action code in ngrx/store library, actually, Action interface defined as following.

export interface Action {
    type: string;
}

I searched internet a lot about new ngrx/store, however I was not able to find answer, does anyone have some information about it?

Upvotes: 2

Views: 2799

Answers (3)

Noumaan Shah
Noumaan Shah

Reputation: 1

A payload property can be added to your Action.

If your Action has payload:

export class ActionWithPayload implements Action {
   readonly type = 'Action With Payload';
   constructor(payload: string) {}
}

Upvotes: -1

GHB
GHB

Reputation: 947

actually it's been removed from the new versions of ngrx/store. but there are some ways that you can deploy it yourself. check this github issue, people have come up with different useful solutions.

that being said, my favourite solution is extending that Action interface into a new generic interface like this:

export interface NewAction<T> extends Action {
    type: string;
    payload: T;
}

and then you can use it like:

ExampleAction: NewAction<boolean> = {
    type: 'myType',
    payload: true
};

Upvotes: 3

Barry Tormey
Barry Tormey

Reputation: 3116

Checkout the migration docs https://github.com/ngrx/platform/blob/master/MIGRATION.md

They note that the payload property on the interface has been removed from Action and you need to cast the action to the appropriate type to access the payload property

Upvotes: 0

Related Questions