piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

Object map style cannot use getType to match action in the createReducer #214

Open cloudshadow opened 4 years ago

cloudshadow commented 4 years ago

Description

When i use object map style to create reducer i find ts can not get the type of action and state.

Mandatory info

How to Reproduce

CodeSandbox Link

(PASTE HERE your codesandbox link)

No CodeSandbox Link

If no codesandbox, then please provide a full working code example below including actions, reducers and your custom types used in the example.

// this code is not work
export const reducer = createReducer<ITypeState, ITypeActions>(defaultTypeState, {
  [getType(rootAction.someActions.success)]: (state, action) =>
    produce(state, draft => {
      // todo
    })
});

// this code is work
export const reducer = createReducer<ITypeState, ITypeActions>(defaultTypeState, {
  ['Success']: (state, action) =>
    produce(state, draft => {
      // todo
    })
});

Expected behavior

when use getType function in object map style ts can get the correct type for action and state

Suggested solution(s)

Project Dependencies

piotrwitek commented 4 years ago

Hi @cloudshadow Report is invalid because it's missing info bug I guess rootAction.someActions.success is not a correct string literal type.

Please follow the bug report template and I will reopen the issue.

provide a full working code example below including actions, reducers and your custom types used in the example.

cloudshadow commented 4 years ago

Hi Piotrwitek i think rootAction.someActions.success is correct

piotrwitek commented 4 years ago

@cloudshadow Ok I have found the reason, it's a reported bug in TypeScript: https://github.com/microsoft/TypeScript/issues/29718

I would recommend to go there and ask in the issue when are they planning to fix it to show the interest coming from the community.

Related issue: #140

TODO: I have to update the documentation to highlight the bug and link to the TypeScript issue