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

52
Vistas
Typescript Interface specification with hash

I have this interface where I basically want to have an array of hashes. Something like this (propably not correct):

export interface EntitySpec {
  originId: EntityType;
  mandatoryProperties: Array<{ [key: string]: string }>;
}

But I want to apply the interface like this:

const spec: EntitySpec = {
  originId: 1,
  mandatoryProperties: {
    'code': 'sad',
    'name': 'this',
    'comment': 'here',
  },
};

But I get this: Type '{ code: string; }' is not assignable to type '{ [key: string]: string; }[]'. How would I do this properly?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It's because mandatoryProperties is an Array of objects. Wrap that into [] and you should be fine:

const spec: EntitySpec = {
  originId: 1,
  mandatoryProperties: [
    {
      'code': 'sad',
      'name': 'this',
      'comment': 'here',
    }
  ]
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to assign an object to mandatoryProperties, remove Array<> from an interface as follows:

export interface EntitySpec {
  originId: EntityType;
  mandatoryProperties: { [key: string]: string };
}

const spec: EntitySpec = {
  originId: 1,
  mandatoryProperties: {
    'code': 'sad',
    'name': 'this',
    'comment': 'here',
  },
};

else wrap the mandatoryProperties inside an array as follows:

export interface EntitySpec {
  originId: EntityType;
  mandatoryProperties: Array<{ [key: string]: string }>;
}

const spec: EntitySpec = {
  originId: 1,
  mandatoryProperties: [{
    'code': 'sad',
    'name': 'this',
    'comment': 'here',
  }],
};

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your mandatoryProperties is an object, not an array. You need to remove that Array<>

export interface EntitySpec {
  originId: EntityType;
  mandatoryProperties: { [key: string]: string };
}

However, if you need an array,then you can add [] at the end:

export interface EntitySpec {
  originId: EntityType;
  mandatoryProperties: { [key: string]: string }[];
}

const spec: EntitySpec = {
  originId: 1,
  mandatoryProperties: [
    {
      'code': 'sad',
      'name': 'this',
      'comment': 'here',
    }
  ]
};
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