Adding dynamic properties to class with static properties

Viewed 55

I'm trying to allow dynamic properties in my TS class like so.

export class Finding {
  id?: number;

  title = '';

  assets?: Asset[] = [];

  createdAt = Math.floor(Date.now() / 1000);

  [dynamicField: string]: DynamicFieldTypes;
}

But it gives me the following error.

Property ‘title’ of type ‘string’ is not assignable to ‘string’ index type ‘DynamicFieldTypes’.ts(2411) View Problem

I assume it's because [dynamicField: string] overrides the types of all other properties. How do I fix that?

Inheritance doesn't fix that either

link to playground

0 Answers
Related