Skip to content

Alternate: Add ability for slices to listen to other actions #86

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

Merged
merged 6 commits into from
Jan 25, 2019
Merged

Alternate: Add ability for slices to listen to other actions #86

merged 6 commits into from
Jan 25, 2019

Conversation

Dudeonyx
Copy link
Contributor

@Dudeonyx Dudeonyx commented Jan 22, 2019

Implemented my proposed changes from #83 (comment)

@denisw @markerikson your inputs are appreciated

Note: this PR is for the feature/other-slice-action branch not master

  • Removed getType() utility

  • slice is no longer attached to actionsMap and reducerMap

  • Removed reducerMap and directly forward reducers to createReducer

  • createAction() action creators type property returns a string literal for better type saftey

  • Fixed tests

  • Added tests

@netlify
Copy link

netlify bot commented Jan 22, 2019

Deploy preview for redux-starter-kit-docs ready!

Built with commit 09abf7b

https://deploy-preview-86--redux-starter-kit-docs.netlify.com

@markerikson
Copy link
Collaborator

Per my comment in the other thread, this appears to be removing functionality that I want to keep. I don't think this is the direction I want to go.

@Dudeonyx
Copy link
Contributor Author

@markerikson In that case, I've reverted the changes so slice is still attached to the action creators.

The only changes here are to the typing of createAction() action creator type and toString() properties, making the them string literals instead of generic strings

@markerikson
Copy link
Collaborator

Okay. one other question. @Jessidhia mentioned something in Reactiflux about &-ing the action creator with a string, possibly in the return value of createAction:

[7:13 PM] Jessie : acemarke to the return type of createAction; it will probably need an as cast
[7:13 PM] Jessie : const result = ...; return result as typeof result & string

Would that allow the action creator itself to be used directly as a key in an object, without a TS2464 error like microsoft/TypeScript#14207 ?

@Dudeonyx
Copy link
Contributor Author

Dudeonyx commented Jan 22, 2019

@markerikson I've tried that and while doing something like

export function createAction<P = any, T extends string = string>(
  type: T
): PayloadActionCreator<P, T> & T {  // <- note the `& T`
  function actionCreator(): Action<T>
  function actionCreator(payload: P): PayloadAction<P, T>
  function actionCreator(payload?: P): Action<T> | PayloadAction<P, T> {
    return { type, payload }
  }

  actionCreator.toString = (): T => `${type}` as T

  actionCreator.type = type

  return actionCreator as any // <- typecast as any to prevent errors
}

allows you to use the action creator as object key without TS errors. Typescript will just treat it like a generic string in that case which breaks type safety.

const reducers = {
  [increment]: (state,payload) => state + payload
}

// reducers will have type { [x:string]: (number, number) => number }

So I think it's better for TS users to explicitly use the action creator type property or toString() method which now return string literals. Of course JS users are unaffected by this.

@markerikson markerikson merged commit 26b94d4 into reduxjs:feature/other-slice-actions Jan 25, 2019
markerikson added a commit that referenced this pull request Jan 25, 2019
* Add `type` field to action creators

* Update createSlice to handle other actions

* Fill out createSlice docs

* Formatting

* Hacky attempt to fix createAction type tests

* Fix example typo

* Alternate: Add ability for slices to listen to other actions (#86)

Implemented my proposed changes from #83 (comment)

@denisw  @markerikson your inputs are appreciated

*Note: this PR is for the `feature/other-slice-action` branch not `master`*

* ~~Removed `getType()` utility~~

* ~~`slice` is no longer attached to `actionsMap` and `reducerMap`~~

* ~~Removed `reducerMap` and directly forward `reducers` to `createReducer`~~

- [x] `createAction()` action creators `type` property returns a `string literal` for better type saftey

- [x] Fixed tests

- [x] Added tests

* Play with type tests a bit
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.

2 participants