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

105
Vistas
how to make an iterable type in type script that has a key value pair

I am using type script and I want to make a type that represents an object like this the keys generated are genrated dynamically how do I do that

{  dog:true,
cat:true,
x:true
}

currently I am just using any but I woud like a proper type

 const matches: any= {}

i get this error when i try to use


 {[key: string]: boolean}
Type 'string[] | { [key: string]: boolean; }' must have a '[Symbol.iterator]()' method that returns an iterato

code that causes this error

const docgetter=()=>
    const matches: { [key: string]: boolean } = {}
    const documentFieldKeys = Array.isArray(documentNames) ? documentNames : Object.keys(documentNames)
  

    return [matches, documentFieldKeys]

}



const [matches,kycKeys]=docgetter()

for(key of kycKeys)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Using [key : string]

type dict = {
    [key : string] : boolean
}

const o: dict = {
    cat: true,
    dog: true,
    x: false
}

Edit:

you didn't specify a type for the output of docgetter so typescript inferred it incorrectly as

const kycKeys: {
    [key: string]: boolean;
} | string[]

You could either fix this by doing

for(const key of kycKeys as string[]) {
  console.log(key)
}

to let typescript know you are iterating an array and not an object (object would error)

Or you could let typescript know what the output is

fixed version of your code:

const documentNames = {
  "a": true,
  "b": true,
  "c": true
}

type output = [
  { [key: string]: boolean },
  string[]
]

const docgetter=() : output => {
    const matches: { [key: string]: boolean } = {}
    const documentFieldKeys : string[] = Array.isArray(documentNames) ? documentNames : Object.keys(documentNames)
    return [matches, documentFieldKeys]

}


const [matches,kycKeys]=docgetter()

for(const key of kycKeys) {
  console.log(key)
}
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