Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
In Redux Toolkit, TypeScript doesn't catch the wrong type of state

import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface InfoState {
    name : string
    age : number
    email : string
}

const initialState = { name: '', age: 0, email: '' } as InfoState 

const userSlice = createSlice({
    name: 'user',   
    initialState: { value: initialState },
    reducers: {        
        login(state, action: PayloadAction<InfoState>) {
            console.log(typeof(action.payload.age));
            state.value = action.payload
        },
        logout(state) {
            state.value = { name: '', age: 0, email: ''  }
        },
    },
})

export const { login } = userSlice.actions
export const { logout } = userSlice.actions
export default userSlice.reducer

There is a file of typescript from the Redux Toolkit.

dispatch(login({ name: 123, age: "20", email: "email@gmail.com" }))

I have dispatched the following values ​​in javascript

It is different from the type set in TypeScript, but the state is changed. I want to know why.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

TypeScript only exists in your editor to tell you where you are doing wrong. In your browser - or JavaScript for that matter - TypeScript annotations have no meaning. TypeScript will not stop you at runtime from doing something stupid, none of your TypeScript annotations exist in the user's browser any more.

If you want to have run-time-checking, you have to write that by hand. That's neither TypeScript's job not Redux-Toolkit's job.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda