1

given following object:

const myConfig = {
  'keyA': { provider: 'Country', validators: [] }, // see RuleDetailsFormBuilderConfiguration type
  'myGroup': [
    {
      'keyB': { provider: 'Country', validators: [] },
      'keyC': { provider: 'Country', validators: [] },
      'keyD': { provider: 'Country', validators: [] },
    }
  ]
};

interface Test  {
  keyA: any;
  keyB: any;
  keyC: any;
  keyD: any
}

How would the type/interface for myConfig look like in TypeScript? Basically I want to have a combination of [key of generic] : RuleDetailFieldConfig. Additionally it should be possible to define a arbitrary key (in my example myGroup and give an array of the combination [key of generic] : RuleDetailFieldConfig. For a concrete example of a generic see the above mentioned interface Test

I'm able to make a type definition for the first line in the object ('keyA': { provider: 'Country', validators: [] })

For this my type looks like this:

export interface RuleDetailFieldConfig {
  provider: string;
  validators: string[];
}

export type RuleDetailsFormBuilderConfiguration<Type> = {
  [Property in keyof Type ]: RuleDetailFieldConfig
};

For this part:

'myGroup': [
    {
      'keyB': { provider: 'Country', validators: [] },
      'keyC': { provider: 'Country', validators: [] },
      'keyD': { provider: 'Country', validators: [] },
    }
  ] // see RuleDetailsFormGroupBuilderConfiguration

My type looks like this:

export type RuleDetailsFormGroupBuilderConfiguration<Type> = {
  [groupname: string] : RuleDetailsFormBuilderConfiguration<Partial<Type>>[]
}

How can I combine these two separate types to have one type for my object?

Mikelgo
  • 483
  • 4
  • 15
  • It is a bit unclear for me what are you trying to achieve – captain-yossarian from Ukraine Dec 01 '21 at 09:51
  • 1
    Thanks for the hint. I will rephrase and try to make it more clear. just give me some minutes – Mikelgo Dec 01 '21 at 09:54
  • rephrased. hope it is better now – Mikelgo Dec 01 '21 at 14:10
  • Unfortunately this looks like a generalization of [this issue](https://stackoverflow.com/q/61431397/2887218) where you have some known keys whose values are some type and some unknown keys whose values are some incompatible type. This is not directly representable in TypeScript. It's similar to `RuleDetailsFormBuilderConfiguration & RuleDetailsFormGroupBuilderConfiguration` but that doesn't work for the reason intersections don't work as that answer mentions. If that's the case, I don't think I'm going to rewrite that answer here. Unless, maybe you can refactor? – jcalz Dec 01 '21 at 16:28
  • If you could always make exactly one key named `myGroup`, then that would work. If you need an unknown number of keys, then could you push it down into a `groups` subproperty like `groups: {myGroup: ...; someOtherGroup: ...}`? If you could do that then you have a straightforward intersection type. – jcalz Dec 01 '21 at 16:30
  • 1
    @jcalz thanks for your comment. I already thought it is not possible. Thanks for the linked issue, that's kind of my problem – Mikelgo Dec 02 '21 at 06:50

0 Answers0