Saya mengikuti contoh Dynamic Dataset dari vis dan saya mendapatkan kesalahan TypeScript ketika saya mencoba mengatur tepi.

Saya hanya memiliki array tepi yang terlihat seperti:

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

Dan saya menyetel data sebagai

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

Kesalahan yang saya dapatkan ada di tepi.

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>>[]'.

Saya menggunakan vis-network versi 5.2.4.

3
lmcadory 19 September 2019, 19:49

1 menjawab

Jawaban Terbaik

Ini adalah perilaku TS standar. Anda mencoba menetapkan tipe A ke B tetapi mereka tidak memiliki kesamaan. DataSet dapat menerima item yang memiliki id opsional. Namun tepi Anda tidak memiliki id dan TS hanya mengatakan Anda melakukan sesuatu yang salah.

Solusi satu:

import { Edge } from "vis-network";

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

Solusi dua:

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

Solusi tiga:

const edgesArray: { id?: undefined, from: number, to: number }[] = [
    { from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 3, to: 5 },
];
5
Tomáš Vyčítal 20 September 2019, 17:07