1

I'm using javascript to create an object and want to add in an interface for the data:

Javascript:

const childGroups: Children = {};
      childGroups.children = [];

// Pushing some data 
childGroups.children.push(children);

Interface:

export interface Child {
    ClusterPoint
}

export interface Children {
    children: {
        [key: number]: Child
    }
}

I'm getting the below errors: Property 'children' is missing in type '{}' but required in type 'Children'. Property 'push' does not exist on type {[key: number]: Child}

The data looks this:

enter image description here

Any help would be appreciated.

Update

Thanks to Nikita Madeev, I managed to get it to work with this:

export interface Child {
    children: {
        ClusterPoint
    };
}

export interface Children {
    children: Child[];
}

2 Answers 2

5
export interface Child {
    ClusterPoint;
}

export interface Children {
    children: Child[];
}

const childGroups: Children = { children: [] };

// Pushing some data
childGroups.children.push(newChildren);
  1. Children.children - object, not array, object has not push method
  2. During initialization, you need to specify all the necessary properties
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. It didn't work 100%, had to change Child interface, will add it to my question.
0

If you want to achieve this structure

const childGroups: Children = {};
      childGroups.children = [];

then interfaces would be

export interface Children {
    children?: Child[];
}

and if you want to achieve this structure

const childGroups: Children = {};
      childGroups.children = {[some_id]: some_childValue};

then interface would be

export interface Children {
    children?: {
      [key: number]: Child
     };
}

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.