0

i'm trying to conform an object to a interface but i'm facing issues

export interface Rule {
  id: string
  properties: Array<{ [key: string]: number }  & { children: Array<string> }>
}
export interface Step {
  rules?: Rule[]

}

const step: Step ={
  rules: [
    {
      id: 'aaa',
      properties: [
        {
          children: ['aa','vv'],
          "toolset": 13
        }
      ]
    }
  ]
}

i get this error

Type '{ children: string[]; toolset: number; }' is not assignable to type '{ [key: string]: number; } & { children: string[]; }'.
  Type '{ children: string[]; toolset: number; }' is not assignable to type '{ [key: string]: number; }'.
    Property 'children' is incompatible with index signature.
      Type 'string[]' is not assignable to type 'number'.

if i change the properties type into properties: Array<{ [key: string]: number } | { children: Array<string> }>

i get no warnings from the linter, but children and the other props need to be together

p.s i also tried to change Rule into

export type Rule=  {
  id: string
  properties: Array<{
   children: Array<string>;
     [key: string]: number;
    } >
}

but the linter gives me

Property 'children' of type 'string[]' is not assignable to 'string' index type 'number'
majkl zumberi
  • 156
  • 1
  • 8
  • I dont understand this line _"i get no warnings from the linter, but children and the other props need to be together"_ - that seems like the right way to solve the issue to me - with `|` – Jamiec Oct 03 '22 at 08:25

0 Answers0