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

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

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 Report

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 Report

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