0

I have a react component and within that react component, i am using blueprint.js Tree fucntion to render a tree based off a json object returned from the server. I am getting a type issue that I am trying to fix, but I am not sure where it is coming from... this is the error:

Type 'ITreeNode<{}>[] | undefined' is not assignable to type 'ITreeNode<{}>[]'.
  Type 'undefined' is not assignable to type 'ITreeNode<{}>[]'.ts(2322)
HelpTree.tsx(11, 5): The expected type comes from property 'nodes' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<HelpTree> & Readonly<State> & Readonly<{ children?: ReactNode; }>'

I am not sure why i am getting this error or how to fix it.

here is the source code. I am using the Tree from bluepring and i am assigning the ITreeNode after it is build dynamically.

export const HelpDialog: React.FC = () => {
    const themeClass = useBehavior(mainStore.themeClass);
    const isOpen = useBehavior(mainStore.isHelpDialogVisible);

    const [files, setFiles] = useState([]);

    const [folderLoading, setFolderLoading] = useState(false);

    const InitialState: ITreeNode[] = [];

    let currentNode: HelpDTO;
    let parentFolder: any = {};
    let majorFolder: any = {};
    let majorFile: any = {};

    useEffect(() => {
        if (isOpen) {
            fetchHelp();
        }
    }, [isOpen]);

    const fetchHelp = () => {
        helpApi.getHelpFiles().then((response) => {
            if (response.status !== 200) return;
            setFiles(response.data);
            checkFileContent();
        });
    };

    return (
        <Dialog
            className={classNames(themeClass, styles.helpDialog)}
            title="Help"
            icon="help"
            isOpen={isOpen}
            onOpened={fetchHelp}
            onClose={closeDialogBox}
        >
            <div style={{ overflowY: "scroll" }}>
                {folderLoading ? <HelpTree nodes={setNodes(files)} /> : <Spinner />}
            </div>
        </Dialog>
    );
};

the error is coming from the 5th from last line:

{folderLoading ? <HelpTree nodes={setNodes(files)} /> : 

where nodes is decalred:

this is where the tree is declared:

interface Props {
    nodes: ITreeNode[];
}

export interface State {
    nodes: ITreeNode[];
}

// use Component so it re-renders everytime: `nodes` are not a primitive type
// and therefore aren't included in shallow prop comparison
export class HelpTree extends React.Component<State, Props> {
    constructor(props: Props) {
        super(props);
        this.state = {
            nodes: props.nodes
        };
    }

    render() {
        return (
            <Tree
                contents={this.state.nodes}
                onNodeCollapse={this.handleNodeCollapse}
                onNodeExpand={this.handleNodeExpand}
                onNodeDoubleClick={this.handleNodeDoubleClick}
            />
        );
    }
}
Omar_Jandali
  • 485
  • 2
  • 7
  • 18

1 Answers1

0

The error is telling you that your setNodes function may return "undefined" instead of ITreeNode.

You can either change the type where your tree is declared, by replacing this:

interface Props {
    nodes: ITreeNode[];
}

with this:

interface Props {
    nodes: ITreeNode[] | undefined;
}

Or you can check your setNodes to make sure it never returns "undefined"

Bruno Monteiro
  • 4,153
  • 5
  • 29
  • 48