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

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

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 Denunciar

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 Denunciar

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