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

createSlice actions TypeScript typings for the payload does not have Intellisense support #426

Closed
RPDeshaies opened this issue Mar 11, 2020 · 6 comments

Comments

@RPDeshaies
Copy link

Context

When using the createSlice api with Typescript, it generates actions that can be called using two overloads

  1. payload?: undefined
  2. <PT extends MY_PAYLOAD>(payload?: PT) where MY_PAYLOAD would be the typing provided inside the PayloadAction of the reducer

I have the impression that because of that, Typescript is not able to generate the autocomplete properly even though it is still able to validate the object.

Example

const mySlice = createSlice({
  name: "mySlice",
  initialState: {
    foo: 3
  },
  reducers: {
    setFoo(
      state,
      action: PayloadAction<{
        foo: number;
      }>
    ) {
      state.foo = action.payload.foo;
    }
  }
});

mySlice.actions.setFoo(); // is valid
mySlice.actions.setFoo({}); // is not valid 

The error message you get is

image

Which is perfect, but if you try to toggle the intellisense inside the anonymous object of the second call to setFoo you get this:

image

Expected behavior

We should be able to see that the only property the payload can receive is foo: number

Versions

"@reduxjs/toolkit": "1.2.5",
"typescript": "3.7.2",
@RPDeshaies RPDeshaies changed the title createSlice actions payload typings doesnt provide autocomplete even though it validates it createSlice actions TypeScript typings for the payload does not have Intellisense support Mar 11, 2020
@phryneas
Copy link
Member

I guess you have strictNullChecks: false in your tsconfig.json?

Please see this answer

@RPDeshaies
Copy link
Author

It's turned off by default and turning it on does indeed fix the issue.
Just read the other Issue. Thanks for looking into it.

Closing the issue

@phryneas
Copy link
Member

phryneas commented Mar 11, 2020

Every tsconfig.json generated by tsc --init since TypeScript 2.3 should have strict: true, which turns on all the strict options. (See the release notes of TS 2.3).
It's only off by default to preserve backwards compatibility with pre-2017-code. You might want to check your other strict settings ;)

@phryneas
Copy link
Member

Hey @RPDeshaies, I put in a PR that might improve the autocompletion for people with strictNullChecks: false (#428) - could you give it a test and give me feedback if it improves the situation to before?

yarn add https://pkg.csb.dev/reduxjs/redux-toolkit/commit/982da2ca/@reduxjs/toolkit
npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/982da2ca/@reduxjs/toolkit

@RPDeshaies
Copy link
Author

I'll check it out today!

@RPDeshaies
Copy link
Author

This works like a charm @phryneas

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