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

171
Visualizações
Property 'previous' is missing in type - TypeScript + ReactJS

Quite new to reactjs and ts. I know why I am having this error, but I am not sure what would be the best fix for this

Currently using reactjs created an

interface

interface IPropertyTax {
  annul: {
    current: number;
    previous: number;
  };
  monthly: {
    current: number;
    previous: number;
    difference: number
  };
}

useState

    const [propertyTax, setPropertyTax] = useState<IPropertyTax>({
        annul: {
            current: 0,
            previous: 0
        },
        monthly: {
            current: 0,
            previous: 0,
            difference: 0
        }
    });

this action will be done trying to set the state

        setPropertyTax({
            ...propertyTax,
            annul: { current: validatedValue },
            monthly: { current: monthlyPropertyTax }
        });

Then I would get an error of Property 'previous' is missing in type '{ current: number; }' but required in type '{ current: number; previous: number; }'

I know it's because I didn't add previous since in my interface it's a required field.

So I thought maybe I should make them options such as

interface IPropertyTax {
  annul: {
    current?: number;
    previous?: number;
  };
  monthly: {
    current?: number;
    previous?: number;
    difference?: number
  };
}

but this would lead to propertyTax.monthly.current might be Object is possibly 'undefined' which I could just do (propertyTax.monthly.current || 0)

The above would work, but I didn't know if that's the a good option?

I thought of another way might be

        setPropertyTax({
            ...propertyTax,
            annul: { current: validatedValue, previous: propertyTax.annul.previous },
            monthly: { current: monthlyPropertyTax, previous: propertyTax.monthly.previous, difference: propertyTax.monthly.difference }
        });

But again, any of these are good options? or there is a better way to work this out?

Thanks in advance for any help.

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

0

As u are not setting few properties, u are getting this error.

if u are trying to update the annual.current property then u can do like this

const newPropertyTax = propertyTax;
newPropertyTax?.annual?.current = validatedValue;
setPropertyTax(newPropertyTax);
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