|
8 | 8 | } from 'd3-force'; |
9 | 9 | import type { Snippet } from 'svelte'; |
10 | 10 |
|
11 | | - type Forces = Record<string, Force<any, any>>; |
| 11 | + export type Forces< |
| 12 | + NodeDatum extends SimulationNodeDatum, |
| 13 | + LinkDatum extends SimulationLinkDatum<NodeDatum> | undefined, |
| 14 | + > = Record<string, Force<NodeDatum, LinkDatum>>; |
12 | 15 |
|
13 | 16 | export type Data<TNode = any, TLink = any> = { |
14 | 17 | nodes: TNode[]; |
|
60 | 63 | LinkDatumFor<NodeDatum, LinkDatum> |
61 | 64 | >; |
62 | 65 |
|
63 | | - export type ForceSimulationProps<NodeDatum, LinkDatum> = { |
| 66 | + export type ForceSimulationProps< |
| 67 | + NodeDatum extends SimulationNodeDatum, |
| 68 | + LinkDatum extends SimulationLinkDatum<NodeDatum> | undefined, |
| 69 | + > = { |
64 | 70 | /** |
65 | 71 | * Force simulation parameters |
66 | 72 | */ |
67 | | - forces: Forces; |
| 73 | + forces: Forces<NodeDatum, LinkDatum>; |
68 | 74 |
|
69 | 75 | /** |
70 | 76 | * An object with arrays of nodes and links, |
|
148 | 154 | }; |
149 | 155 | </script> |
150 | 156 |
|
151 | | -<script lang="ts" generics="NodeDatum, LinkDatum = undefined"> |
| 157 | +<script |
| 158 | + lang="ts" |
| 159 | + generics="NodeDatum extends SimulationNodeDatum, |
| 160 | + LinkDatum extends SimulationLinkDatum<NodeDatum> | undefined," |
| 161 | +> |
152 | 162 | import { watch } from 'runed'; |
153 | 163 | import type { Prettify } from '@layerstack/utils'; |
154 | 164 |
|
|
187 | 197 |
|
188 | 198 | // d3.Simulation does not provide a `.forces()` getter, so we need to |
189 | 199 | // keep track of previous forces ourselves, for diffing against `forces`. |
190 | | - let previousForces: Forces = {}; |
| 200 | + let previousForces: Forces<NodeDatum, LinkDatum> = {}; |
191 | 201 |
|
192 | 202 | let paused: boolean = true; |
193 | 203 |
|
|
291 | 301 | simulation.nodes(nodes); |
292 | 302 | } |
293 | 303 |
|
294 | | - function pushForcesToSimulation(forces: Forces) { |
| 304 | + function pushForcesToSimulation(forces: Forces<NodeDatum, LinkDatum>) { |
295 | 305 | // Evict obsolete forces: |
296 | 306 | const names = Object.keys(previousForces); |
297 | 307 | for (const name of names) { |
|
0 commit comments