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

0

1 Answer 1

2

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

interface MappingConfiguration {
  required: string[];
  configurations?: Record<string, Configuration>;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay. You are right. A new property is most likely the better alternative. Thank you for your feedback! :)
You are welcome. Feel free to accept my answer :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.