Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

javascript - 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.

question from:https://stackoverflow.com/questions/66054914/typescript-record-error-when-using-pointer-like-tree

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...