Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem setting edges in vis-network with typescript

I'm following the Dynamic Dataset example from vis and I'm getting a typescript error when I try to set edges.

I just have an edge array that looks like:

edgesArray = [
 { from: 1, to: 3 },
 { from: 1, to: 2 },
 { from: 2, to: 4 },
 { from: 3, to: 5 },
 ]

and I'm setting the data as

let data = {
  nodes: new vis.DataSet(nodesArray),
  edges: new vis.DataSet(edgesArray)
}

The error I'm getting is in the edges.

No overload matches this call.
  Overload 1 of 2, '(options?: DataSetInitialOptions<"id">): DataSet<Partial<Record<"id", string | number>>, "id">', gave the following error.
    Type '{ from: number; to: number; } []' has no properties in common with type 'DataSetInitialOptions<"id">'.
  Overload 2 of 2, '(data: Partial<Record<"id", string | number>>[], options?: DataSetInitialOptions<"id">): DataSet<Partial<Record<"id", string | number>>, "id">', gave the following error.
    Argument of type '{ from: number; to: number; } []' is not assignable to parameter of type 'Partial<Record<"id", string | number>>[]'.

I'm using version 5.2.4 of vis-network.

like image 367
lmcadory Avatar asked Jun 30 '26 06:06

lmcadory


1 Answers

This is standard TS behavior. You're trying to assign type A into B but they have nothing in common. DataSet can accept items that have optional id. However your edges have no ids and TS simply says you're doing something wrong.

Solution one:

import { Edge } from "vis-network";

const edgesArray: Edge[] = [
    { from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 3, to: 5 },
];

Solution two:

const edgesArray = [
    { id: undefined, from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 3, to: 5 },
];

Solution three:

const edgesArray: { id?: undefined, from: number, to: number }[] = [
    { from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 3, to: 5 },
];
like image 64
Tomáš Vyčítal Avatar answered Jul 05 '26 06:07

Tomáš Vyčítal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!