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

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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