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

48
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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