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

76
Visualizações
Array of objects which have all the same properties

I was wondering, if it's possible to ensure with Angular 12 / Typescript, that all objects within an array have the same properties, without declaring the properties explicitly (should be generic).

Here's an example of what I want to achieve:

// This should compile because all objects have the same properties
const correct = [
 {firstname: 'Jack', lastname: 'Sanders'}, 
 {firstname: 'Susanna', lastname: 'Marsaglia'},
 {firstname: 'Walter', lastname: 'Walker'}
]

// This shouldn't compile because some object have different properties
const wrong = [
 {firstname: 'Aline', lastname: 'Rose', phone: 'xxx-xxx-xx-xx'},
 {firstname: 'Daniel', email: 'dan@gmail.com'},
 {firstname: 'Louisa', email: 'louisa@icloud.com'}
]

I was able to create an Interface which only allows properties with type string, but I couldn't achieve the example above.

export interface Data {
 [keys: string]: string
}

Update (Use-Case)

I need this check in a service which exports data. You should be able to pass an array of objects, which represent rows within a table.

For example:

const data = [
 {firstname: 'Jack', lastname: 'Sanders'}, 
 {firstname: 'Susanna', lastname: 'Marsaglia'},
]

this.exportService.export(data);

This code would generate a file with a table like this:

firstname lastname
Jack Sanders
Susanna Marsaglia

The service should support any passed data, because it's used widely over our entire application. But to be sure that the service can export the data to a table, we need to ensure, that all passed objects have the same properties (e.g. columns).

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

0

function sameKeys(input) {
  if (!input || input.length === 0) return;
  const keys = new Set(Object.keys(input[0]));

  return input.every((item) => Object.keys(item).every((key) => keys.has(key)));
}

If you want a compile time check, then doing a proper type definition makes sense have given in another answer. But that will only work if this data is hardcoded. if you are fetching it from backend it will not work as types are lost when the code is already build as javascript don't have types.

about 4 years ago · Juan Pablo Isaza Relatório

0

You should achieve this by interface as below

interface IStructure {
    firstname: string;
    lastname: string;
  } 

const correct: Array<IStructure> = [
 {firstname: 'Jack', lastname: 'Sanders'}, 
 {firstname: 'Susanna', lastname: 'Marsaglia'},
 {firstname: 'Walter', lastname: 'Walker'}
];

//wrong will throw error
about 4 years ago · Juan Pablo Isaza Relatório

0

const data = [
 {firstname: 'Jack', lastname: 'Sanders'}, 
 {firstname: 'Susanna', lastname: 'Marsaglia'},
 {firstname: 'Walter', lastname: 'Walker'}
]

const dataReference : Required<typeof data[number]>[] = data
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