Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!