Почему TS выдает ошибку при попытке присвоить объект, ведь в типе указано, что это null или тип User?
state = {
id: token.user_id,
name: token.user_name,
role: token.user_role
}
Type '{ id: number; name: string; role: string | null; }' is not assignable to type 'null'.
import { createSlice } from '@reduxjs/toolkit'
interface User {
id: string;
name: string;
role: string;
}
interface Token {
user_id: number;
user_name: string;
user_role: string | null;
exp: number;
iat: number;
}
const initialState: User | null = null
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
setUser: (state, action) => {
const access_token = action.payload.split('Bearer ')[1]
const token: Token = jwtDecode(access_token)
state = {
id: token.user_id,
name: token.user_name,
role: token.user_role
}
},
removeUser: state => state = null
}
})
export const { setUser, removeUser } = userSlice.actions
export const userReducer = userSlice.reducer