I have some interfaces in redux-models.ts file:
export interface IUser{
user:{}
}
export interface IUsers{
users:IUser[]
}
export type TUser=IUser|IUsers
And I have called interfaces in todoSlice.ts file:
import { createSlice,PayloadAction } from "@reduxjs/toolkit";
import { TUser,IUser,IUsers } from "./redux-models";
const initialState:TUser={
user:{},
users:[]
}
const slice=createSlice({
name:"slice",
initialState:{initialState},
reducers:{
setUsers(state,action:PayloadAction<IUsers>){
state.users=action.payload;
}
}
})
But The typescript compile wont let me continue due to error:
Property 'users' does not exist on type 'WritableDraft<{ initialState: TUser; }>'.ts(2339)
Note:(Error happened when I set state.users)
What was wrong?
CodePudding user response:
On interface IUser you don't have 'users', that error has happened because you wrote IUser | IUsers
(also in the IUsers you don't have 'user').
You can try this:
export type TUser = IUser & IUsers