3

i have a question about Typescript interfaces. I've got the following interface:

interface MappingConfiguration {
  [key: string]: Configuration;
  required: string[];
}

interface Configuration {
 a: string;
 b: string;
}

I understand why this doesn't work. The MappingConfiguration interface allows dynamic keys of type Configuration. So any property can be binded on the object. This works as intended. But here's the problem: I also have a property "required", which is a string[]. Typescript does not allow the string[], because it expects the Configuration type. I understand that. So i came up with the following result:

interface MappingConfiguration {
  [key: string]: Configuration | string[];
  required: string[];
}

interface Configuration {
 a: string;
 b: string;
}

This feels totally wrong. Is there maybe another way of combining dynamic and static object keys in an interface? Thanks in advance! :)

mactive
  • 61
  • 2

1 Answers1

2

A first quick idea is to add an additional property to MappingConfiguration:

interface MappingConfiguration {
  required: string[];
  configurations?: Record<string, Configuration>;
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46