0

I'm trying to create an interface for Object, objects, and array of objects.

Sample interface

export interface ErateColumn {
  categories:{
    [key:string]:column[]
  }
}

interface column {
  label:string
  value:string
}

Sample API response like this

{
"categories": {
    "Basic Information": [
        { label: "Applicant Type", value: "ApplicantType" },
        { label: "Organization Name", value: "OrganizationName" },
    ],
    "FRN Lineitem": [
        { label: "Monthly_Cost", value: "Monthly_Cost" },
        ],
    "FRN status": [
        { label: "Purpose Type", value: "PurposeType" },

    ]
}
}

enter image description here

3 Answers 3

1

what you have provided in this._erateColumn.next(columnList) method is columnList. And somehow columnList seems to be a single object of the interface ErateColumn, While your BehaviorSubject has type array of ErateColumn. Change that to ErateColumn or provide array in this._erateColumn.next(columnList) method for your code to work.

EDIT

According to the new screenshot of yours, it says property categories is missing so either make it optional like this

export interface ErateColumn {
  categories?:{
    [key:string]:column[]
  }
}

interface column {
  label:string
  value:string
}

or provide categories.

Sign up to request clarification or add additional context in comments.

2 Comments

#Parth Savaliya, I'have changed to private readonly _erateColumn = new BehaviorSubject<ErateColumn>({} as ErateColumn) error gone. thanks for your help. Ref: stackoverflow.com/questions/42091602/…
I'll still suggest you make fields optional rather than using your approach
0

May be this approach can help someone

You can convert JSON to typescript's class or interface, Using online tool json2ts or if you are using vs code (Visual Studio) use this extension QuickType - Paste JSON as Code

Note: Your JSON should be a valid JSON otherwise you will get errors

Comments

0

Hope this helps...

interface User {
   name: string;
   phone: string;
}

const user: User ==> Object

const usersList: User[] ===> Array of Objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.