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.
This commit is contained in:
Pete Vilter
2017-09-26 18:50:00 -04:00
committed by Mohamed Hegazy
parent a9f4a8a9e6
commit d2260fff79
2 changed files with 7 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
const gDagre = new dagre.graphlib.Graph();
gDagre.setGraph({})
.setDefaultEdgeLabel(() => {})
.setDefaultEdgeLabel(() => ({}))
.setNode("a", {})
.setEdge("b", "c")
.setEdge("c", "d", {class: "class"});

View File

@@ -1,7 +1,10 @@
// Type definitions for dagre 0.7
// Project: https://github.com/cpettitt/dagre
// Definitions by: Qinfeng Chen <https://github.com/qinfchen>, Lisa Vallfors <https://github.com/Frankrike>
// Definitions by: Qinfeng Chen <https://github.com/qinfchen>
// Lisa Vallfors <https://github.com/Frankrike>
// Pete Vilter <https://github.com/vilterp>
// 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;