Robolisk
Robolisk

Reputation: 1782

Typescript record error when using pointer like tree

I have a function that goes through nodes and places them in a tree:

This is the interface type

interface IDataObj { 
    children: IDataObj[],
    frontmatter : { type: string, title: string, path: string},
    name: string,
    relativeDirectory: string,
  }

in which have my pointer and tree:

let tree: Record<string, IDataObj> = {};

and within my createNode function:

let ptr: Record<string, IDataObj> = tree;

At a certain point within createNode have a simple algorithm that decided where to place a node and change the pointer of the tree:

        ptr[path[i]] = ptr[path[i]] || node;
        ptr[path[i]].children = ptr[path[i]].children || {};
        ptr = ptr[path[i]].children; // < this is giving me errors, ptr = 

The last line where ptr now points to the chil

Type 'IDataObj[]' is not assignable to type 'Record<string, IDataObj>'.
  Index signature is missing in type 'IDataObj[]'.

I'm unaware of how to solve this issue. It seems that fact that I'm using the ability of how loosely typed js is that I arrive at this issue.

Upvotes: 0

Views: 161

Answers (1)

ssbarbee
ssbarbee

Reputation: 1712

Have a closer look at the types you have defined. You are saying I want to assign children a variable whose type is an array, to something whose type is essentially an object.

Upvotes: 1

Related Questions