Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
(TS - Redux) How to initialize state property as null: Type 'UserData' is not assignable to type 'null'

I'm having this error: Type 'UserData' is not assignable to type 'null'.

I defined the user state property as user: UserData | null;... So i don't understand why it doesn't let me to initialize the property as null and then assign it a UserData like value: Error Console

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { UserData } from "../../types/user";

interface AppState {
  user: UserData | null;
}

const appSlice = createSlice({
  name: 'app',
  initialState: { user: null },
  reducers: {
    loadUserIntoApp: {
      reducer(state, action: PayloadAction<UserData>) {
        state.user = action.payload; // This is the line with an error
      }
    }
  }
})

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should infer the AppState interface to the initialState

import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { UserData } from "../../types/user";

interface AppState {
  user: UserData | null;
}

const appSlice = createSlice({
  name: 'app',
  initialState: AppState = { user: null },
  reducers: {
    loadUserIntoApp: {
      (state, action: PayloadAction<UserData>) => { //you should add the reducer like this
        state.user = action.payload;
       }
    }
  }
})

go to decumetation for more info

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda