Skip to content

State argument on createSlice is no longer inferred with typescript beta 4.8 and they do not plan to fix it #2543

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

Closed
lukeapage opened this issue Jul 26, 2022 · 6 comments

Comments

@lukeapage
Copy link

See the bug on the typescript repo: microsoft/TypeScript#49307 (comment)

It seems the typescript team will not fix this. Maybe the redux-toolkit types can be fixed to be compatible and still infer? else the examples and documentation need changing to say that the state argument will not be inferred and need to be explicitly provided.

cc/ @markerikson

@markerikson
Copy link
Collaborator

Hmm. @phryneas , you know way more about the createSlice types than I do - any ideas?

@lukeapage
Copy link
Author

In trying to fix this, I've discovered that even in strict mode, nothing enforces that the payload argument of a slice is typed - and it defaults to any. And in trying to fix that, even on 4.7, it ends up requiring the user to type not just the action parameter but the state one too.

@phryneas
Copy link
Member

Essentially, it breaks ValidateSliceCaseReducers. That's not gonna be fun.

@phryneas
Copy link
Member

For historical context why that type is there: #149 (comment)

@lukeapage
Copy link
Author

btw I worked out how to enforce a payload type is always provided:

export type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & {
    [T in keyof ACR]: ACR[T] extends {
        reducer(s: S, action?: infer A): any;
    }
        ? A extends PayloadAction<any>
            ? CaseReducerWithPrepare<S, A>
            : never
        : ACR[T] extends CaseReducer<S, PayloadAction<EmptyObject>>
        ? (state: S) => S
        : CaseReducer<S, PayloadAction<any>>;

For ts 4.7, I think it does the same enforcement that prepare actions match, it does not require a typed state argument and it enforces that the payload can never be any, unknown or a empty object (but the argument can be missing).

Since it forces typing actions on everyone using typescript, I don't know if you want to include it.

Unfortunately though, it does not help with the infer of the state argument in 4.8.

@markerikson
Copy link
Collaborator

Either we fixed this or it isn't enough of an issue that people are complaining :)

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 a pull request may close this issue.

3 participants