Skip to content
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

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

Closed
1 task done
haraldkrischner opened this issue Nov 22, 2019 · 2 comments
Closed
1 task done

Comments

@haraldkrischner
Copy link

haraldkrischner commented Nov 22, 2019

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

  • Are you extending internal types to enable type-free syntax with createReducer?
import { ActionType, StateType } from 'typesafe-actions';

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

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:
{
  "compilerOptions": {
    "esModuleInterop": true,
    "jsx": "react",
    "lib": [
      "dom",
      "es2018"
    ],
    "resolveJsonModule": true
  }
}
@haraldkrischner haraldkrischner changed the title Getting TS2345 Error when I have got Variables in type names Getting TS2345 Error when I have got variables in type names Nov 22, 2019
@piotrwitek
Copy link
Owner

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

@haraldkrischner
Copy link
Author

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.

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

No branches or pull requests

2 participants