From d2260fff79c8adb4e3d4711adb5b44384dce70bd Mon Sep 17 00:00:00 2001 From: Pete Vilter <7341+vilterp@users.noreply.github.com> Date: Tue, 26 Sep 2017 18:50:00 -0400 Subject: [PATCH] Fix dagre types to allow objects to be set as default edge labels (#19945) The tests call `.setDefaultEdgeLabel(() => {})` which is a translation to ES6 of something seen in the example in [Dagre's readme](https://github.com/cpettitt/dagre/wiki#an-example-layout): `g.setDefaultEdgeLabel(function() { return {}; });`. However, the translation is botched: `() => {}` doesn't return an empty object, it returns `undefined`, because `{}` is not an object literal, it's a block with 0 statements. I think this is what motivated the current typing to specify that `void` can be returned by the callback. However, in my experience returning `undefined` from this callback causes the library to crash. Thus I've chagned the tests to say `.setDefaultEdgeLabel(() => ({}))` (i.e. actually return an empty object, which is what the author probably intended), and updated the types accordingly. This works in my project. --- types/dagre/dagre-tests.ts | 2 +- types/dagre/index.d.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/types/dagre/dagre-tests.ts b/types/dagre/dagre-tests.ts index d9a62a107b..ae81c2c896 100644 --- a/types/dagre/dagre-tests.ts +++ b/types/dagre/dagre-tests.ts @@ -1,6 +1,6 @@ const gDagre = new dagre.graphlib.Graph(); gDagre.setGraph({}) - .setDefaultEdgeLabel(() => {}) + .setDefaultEdgeLabel(() => ({})) .setNode("a", {}) .setEdge("b", "c") .setEdge("c", "d", {class: "class"}); diff --git a/types/dagre/index.d.ts b/types/dagre/index.d.ts index 6f008ad1c8..55d98b0535 100644 --- a/types/dagre/index.d.ts +++ b/types/dagre/index.d.ts @@ -1,7 +1,10 @@ // Type definitions for dagre 0.7 // Project: https://github.com/cpettitt/dagre -// Definitions by: Qinfeng Chen , Lisa Vallfors +// Definitions by: Qinfeng Chen +// Lisa Vallfors +// Pete Vilter // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 export as namespace dagre; @@ -11,8 +14,8 @@ export namespace graphlib { edge(id: any): any; nodes(): string[]; node(id: any): any; - setDefaultEdgeLabel(callback: string|(() => string|void)): Graph; - setDefaultNodeLabel(callback: string|(() => string|void)): Graph; + setDefaultEdgeLabel(callback: string|(() => string|object)): Graph; + setDefaultNodeLabel(callback: string|(() => string|object)): Graph; setEdge(sourceId: string, targetId: string, options?: { [key: string]: any }, value?: string): Graph; setEdge(params: {v: string, w: string, name?: string}, value?: string): Graph; setGraph(label: GraphLabel): Graph;