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

208
Visualizações
How to delete all keys from an object in typescript

I am trying to access the keys in an object which is of type FilterType

Here is the interface -

export interface FilterType {
  name?: string[];
  status?: string[];
  brand?: string[];
  categoryAndColour?: {
    [category: string]: string[];
  };
  rating?: string[];
}

Here is the object -

const newState: FilterType = { ...state };

I am trying to have a function which will remove all the keys from newState however whenever I try to map through the object or to a for..in I keep getting similar errors.

I am currently trying this -

for (var key in newState){
        delete newState[key];
      }
      return newState;

But I get the error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

And No index signature with a parameter of type 'string' was found on type

How can I solve this?

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

0

Since you're trying to create a new empty state, just define the newState as a new object:

const newState: FilterType = {};

If you still want to delete all the keys anyway, define the type of key before using it in for..in (TS playground):

let key: keyof FilterType;

for (key in newState){
  delete newState[key];
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

Object.keys(newState).forEach((key) => delete newState[key]);

now, if you run:

console.log(newState)

the expected output will be an empty object:

{ }
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