Skip to content

feat(Store/Effects) Add toPayloadTyped function - #157

Closed
karptonite wants to merge 2 commits into
ngrx:masterfrom
karptonite:patch-add-typed-to-payload
Closed

karptonite wants to merge 2 commits into
ngrx:masterfrom
karptonite:patch-add-typed-to-payload

Conversation

@karptonite

@karptonite karptonite commented Jul 22, 2017 •

Copy link
Copy Markdown
Contributor

(Third time's a charm)
For discussion, this may require some refinement.

But with the newly type safe version of ofType, this opens up the option of a type safe version of toPayload as well. I wasn't able to retrofit toPayload without breaking previous behavior, unfortunately (maybe someone who knows typescript better than my can give it a go?), but I was able to implement a separate toPayloadTyped, which infers the type of the payload based on the Action.

Is this useful? Is there an even cleaner way to implement it?

Note that this doesn't require that a user declare that their actions implement ActionWithPayload (It is probably best to do so, but Typescript can infer that), but it DOES require that they declare the type of their action when they call ofType.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 90.01% when pulling 93e67ac on karptonite:patch-add-typed-to-payload into 27cdd36 on ngrx:master.

Comment thread modules/store/src/models.ts Outdated
type: string;
}

export interface ActionWithPayload<T> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

export interface ActionWithPayload<T> extends Action {
  payload: T;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I had that at some point. I'll fix it.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 90.01% when pulling 7a3c163 on karptonite:patch-add-typed-to-payload into 27cdd36 on ngrx:master.

@karptonite

Copy link
Copy Markdown
Contributor Author

Do not merge this! I think I may have figured out a way to do it without requiring a separate toPayload function.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants