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

233
Views
How do I fix this Typescript error when accessing optional properties in a mapped type?

I'm using a generic mapped type to add meta data to entity types. I get an error when I try to access optional properties in the meta type.

Type definitions:

interface Entity {
  id: number;
  title: string;
}

interface EntityMeta<T> {
  required: {
    [Property in keyof T]?: true;
  };
}

Use:

const entity: Entity = {
  id: 1,
  title: "test"
};

const meta: EntityMeta<Entity> = {
  required: {
    id: true
  }
};

Object.entries(entity).forEach(([key, value]) => {
  console.log(`${key} = ${value}, Is required: ${meta.required[key] ? 'yes' : 'no'}`)
})

The semantics here is that I want to be able to say which subset of entity properties are required and then to access this meta data when I map over entity properties later.

The error is on that last line: meta.required[key]

Error message:

TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ id?: true | undefined; title?: true | undefined; }'.   No index signature with a parameter of type 'string' was found on type '{ id?: true | undefined; title?: true | undefined; }'.

It's easy to access these properties directly:

const isIdRequired = meta.required.id;

But, I need to access them dynamically via a string key.

How do I best fix this in Typescript?

Thanks,

about 4 years ago · Juan Pablo Isaza
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!