Skip to content

Commit 7b7c96d

Browse files
committed
Do not overwrite existing types
* previous versions of d3.HierarchyNode did not have a type definition for x and y at all. * we added a type definition for the type and defined x and y as `number`. This worked because the new and old type definitions were compatible. * type library got an update ans also added the type for x and y, but they added `number | undefined` as type * typescript now found two different type definitions for the same attribute, and threw an error: `TS2687: All declarations of 'x' must have identical modifiers.` We now declare a new local type definition and cast the official d3.HierarchyNode to our extended, internal one at some critical places. CMK-17220 Change-Id: Ifd569a87d8f569a755e5373b2d29460ee75e9090
1 parent 0515673 commit 7b7c96d

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

packages/cmk-frontend/package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cmk-frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@types/d3-flextree": "^2.1.1",
6161
"@types/d3-hexbin": "^0.2.3",
6262
"@types/d3-sankey": "^0.12.1",
63-
"@types/d3-hierarchy": "=3.1.6",
6463
"@types/dc": "^4.2.1",
6564
"@types/jquery": "^3.5.13",
6665
"@types/lodash": "^4.14.191",

packages/cmk-frontend/src/js/modules/nodevis/example_generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ export class LayoutStyleExampleGenerator {
474474

475475
_add_hierarchy_children(hierarchy_raw, cancel_delta, 1);
476476

477-
const hierarchy = d3.hierarchy<NodeData>(hierarchy_raw);
477+
const hierarchy = d3.hierarchy<NodeData>(hierarchy_raw) as NodevisNode;
478478
hierarchy.descendants().forEach(node => {
479479
node.x = 50;
480480
node.y = 50;

packages/cmk-frontend/src/js/modules/nodevis/type_defs.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,15 @@ export interface ContextMenuElement {
320320
children?: ContextMenuElement[];
321321
}
322322

323-
declare module "d3" {
324-
export interface HierarchyNode<Datum> {
325-
data: Datum;
326-
_children?: this[] | undefined | null;
327-
x: number;
328-
y: number;
329-
fx: number | null;
330-
fy: number | null;
331-
force?: number;
332-
use_transition?: boolean;
333-
children_backup?: this[];
334-
}
323+
export interface NodeVisHierarchyNode<Datum> extends HierarchyNode<Datum> {
324+
_children?: this[] | undefined | null;
325+
x: number; // can also be undefined in original type definition
326+
y: number; // can also be undefined in original type definition
327+
fx: number | null;
328+
fy: number | null;
329+
force?: number;
330+
use_transition?: boolean;
331+
children_backup?: this[];
335332
}
336333

337334
// TODO: add class
@@ -343,9 +340,9 @@ export interface LayoutSettings {
343340
}
344341

345342
export class NodeConfig {
346-
hierarchy: d3.HierarchyNode<NodeData>;
343+
hierarchy: NodeVisHierarchyNode<NodeData>;
347344
link_info: NodevisLink[];
348-
nodes_by_id: Record<string, d3.HierarchyNode<NodeData>>;
345+
nodes_by_id: Record<string, NodeVisHierarchyNode<NodeData>>;
349346
constructor(serialized_node_config: SerializedNodeConfig) {
350347
this.hierarchy = this._create_hierarchy(serialized_node_config);
351348
this.nodes_by_id = this._create_nodes_by_id_lookup(this.hierarchy);
@@ -364,10 +361,10 @@ export class NodeConfig {
364361

365362
_create_hierarchy(
366363
serialized_node_config: SerializedNodeConfig
367-
): d3.HierarchyNode<NodeData> {
364+
): NodeVisHierarchyNode<NodeData> {
368365
const hierarchy = d3.hierarchy<NodeData>(
369366
serialized_node_config.hierarchy
370-
);
367+
) as NodeVisHierarchyNode<NodeData>;
371368
// Initialize default info of each node
372369
hierarchy.descendants().forEach(node => {
373370
node._children = node.children;
@@ -379,8 +376,8 @@ export class NodeConfig {
379376
return hierarchy;
380377
}
381378

382-
_create_nodes_by_id_lookup(hierarchy: d3.HierarchyNode<NodeData>) {
383-
const nodes_by_id: Record<string, d3.HierarchyNode<NodeData>> = {};
379+
_create_nodes_by_id_lookup(hierarchy: NodeVisHierarchyNode<NodeData>) {
380+
const nodes_by_id: Record<string, NodeVisHierarchyNode<NodeData>> = {};
384381
hierarchy.descendants().forEach(node => {
385382
nodes_by_id[node.data.id] = node;
386383
});
@@ -396,8 +393,8 @@ export class NodeConfig {
396393
link_info.forEach(link => {
397394
// Reference by id:string
398395
links.push({
399-
source: this.nodes_by_id[link.source],
400-
target: this.nodes_by_id[link.target],
396+
source: this.nodes_by_id[link.source] as NodevisNode,
397+
target: this.nodes_by_id[link.target] as NodevisNode,
401398
config: link.config,
402399
});
403400
});
@@ -409,13 +406,13 @@ export class NodeConfig {
409406
this.hierarchy.descendants().forEach(node => {
410407
if (!node.parent || node.data.invisible) return;
411408
links.push({
412-
source: node,
413-
target: node.parent,
409+
source: node as NodevisNode,
410+
target: node.parent as NodevisNode,
414411
config: {type: "default"},
415412
});
416413
});
417414
return links;
418415
}
419416
}
420417

421-
export type NodevisNode = HierarchyNode<NodeData>;
418+
export type NodevisNode = NodeVisHierarchyNode<NodeData>;

0 commit comments

Comments
 (0)