Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
How can I define a type in Initialstate in Redux-toolkit?

I want to define a type for the initial value in the slice state. But I don't know what to do.

If I input null in singlePost, I get this red line warning.

but I want to specify the type of the initial value of singlePost.

enter image description here

singlePost will contain these values.

enter image description here

this is my code

    // singlePost = {
    //   Comments: [
    //      {id: 46, content: "hello", createdAt: "2022-04-30T08:24:15.000Z", 
    //   }
    //   {id: 47, content: "zz", createdAt: "2022-04-30T08:24:30.000Z",
    // ],

    // Images: [
    //   PostId: 61
    //   createdAt: "2022-04-27T08:17:51.000Z"
    //   id: 6
    //   src: "bd8b355e-cfe8-4c50-be4c-6c4958958fc6.JPEG"
    //   updatedAt: "2022-04-27T08:17:51.000Z"]

    //   Likecomments: [{},{}]

    //   Likers: [{}]

    //   User:{id:2, nickname:'elon'}

    //   content:"helloword"

    //   createdAt:"22022-212"

    //   id: 61,

    //   updatedAt:"asdasd"
    // }


    export const initialState = {

      singlePost: null,

    }
    const postSlice = createSlice({
      name: 'post',
      initialState,
      reducers: {},
      extraReducers: builder =>
        builder

          .addCase(addComment.fulfilled, (state, action: PayloadAction<test>) => {

            const post = state.mainPosts.find(v => v.id === action.payload.PostId);

            state.addCommentLoading = false;
            state.addCommentDone = true;
            state.singlePost.Comments.push(action.payload);
            post.Comments.push(action.payload);
          })

          .addDefaultCase(state => state),
    });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

interface IImage {
  PostId: number;
  createdAt: string;
  id: number;
  //....
}

interface IComment {
  id: number;
  content: string;
  createdAt: string;
}

inteface ISinglePost {
    Comments: IComment[] | null;
    Images?: IImage[];
}

interface IInitialState {
  singlePost: ISinglePost | null;
}

export const initialState: IInitialState = {

  singlePost: null,

}
about 4 years ago · Juan Pablo Isaza Report

0

There are lots of examples in the docs for createSlice.

You need an interface

interface MySliceState {
  singlePost: null | {
    Comments: Array<{
      id: number,
      content: string,
      ...
    }>,
    Images: ...,
    ...
  }
}

and then assert that inialState is of that type.

    const initialState = {

      singlePost: null,

    } as MySliceState 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!