Home > Software engineering >  Convert Json to Typescript
Convert Json to Typescript

Time:09-12

I have interface items.ts:

export interface item{
  Name: string;
  IsSystemItem: string;
  ConfiguredSegments: ConfiguredSegments; 
}

export interface ConfiguredSegments {
  LiveA: LiveA;
}

export interface LiveA {
  Weight: number;
  Id: string;
}

I have a class where i am filling an array of items mockitems.ts :

export const items: item[] = [
  {
    Name: "Default Item",
    IsSystemItem: "yes",
    ConfiguredSegments: ConfiguredSegments.LiveA   // Here it is throwing error "Cannot find name 
                                                       ConfiguredSegments"
  }
]

How to fill ConfiguredSegments field ?

CodePudding user response:

ConfiguredSegments is an interface and not an object so you can not use it as a value. So you need to do

export const items: item[] = [
  {
    Name: "Default Item",
    IsSystemItem: "yes",
    ConfiguredSegments: {
       LiveA: {
           Weight: 123,
           Id: 'yourId'
       }
    }
  }
]

CodePudding user response:

Try this:

export const items: item[] = [
 {
    Name: "Default Item",
    IsSystemItem: "yes",
    ConfiguredSegments: {
        LiveA: {
            Weight: 0,
            Id: 'test id'
        }
    }
 }
]
  • Related