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 99 forks source link

Getting TS2345 Error when I have got variables in type names #213

Closed haraldkrischner closed 4 years ago

haraldkrischner commented 4 years ago

Description

We converted our project to using typesafe actions and it worked well until I touched the last reducer. Having variables in the type names like: export const initSelectionFromState = createAction(SELECTION +' [DOC] INIT_FROM_STATE')() can cause a type error TS2345 in the reducer to occur. It does not happen in all constellations, and it does not on the first handleAction call. Removing the SELECTION variable from that name fixes it

Mandatory info

declare module 'typesafe-actions' { export type Store = StateType<typeof import('../src/redux/store').default>;

export type RootState = StateType<typeof import('../src/redux/reducers/rootReducer').default>;

export type RootAction = ActionType<typeof import('../src/redux/actions').default>;

interface Types {
    RootAction: RootAction;
}

}


- [x ] Did you checked [compatibility notes][1] and [migration guides][2]?

## How to Reproduce

<!-- You have two options, fill in a codesandbox template or provide full working code example. -->

### CodeSandbox Link
https://codesandbox.io/s/typesafe-actions-reference-project-yl65h
see todos/actions.ts and todos/reducer.ts
The error in our project looks slighty different and breaks the whole app, while this seems still to work, but you see that it makes a difference.

## Expected behavior
It should not cause an error, at least I want to understand why this happens… It took me a full day to track it down to this odd cause

## Project Dependencies

- Typesafe-Actions Version: 5.1.0
- TypeScript Version: 3.7.2
- tsconfig.json:
```ts
{
  "compilerOptions": {
    "esModuleInterop": true,
    "jsx": "react",
    "lib": [
      "dom",
      "es2018"
    ],
    "resolveJsonModule": true
  }
}
piotrwitek commented 4 years ago

Hey @haraldkrischner I think you're getting this: Limitations of TypeScript when working with string constants https://github.com/piotrwitek/typesafe-actions#constants

haraldkrischner commented 4 years ago

Thanks for your quick response @piotrwitek Yes, it looks like that is the case. Besides it took me quite a while to narrow it down, the workaround is quite easy. Though we intended to share the actions across entities in the first place. Anyway, this project reduces a lot of IMHO awkward redux typescript boilerplate code and is a big win for our project, so thank you!

Best H.