-
Notifications
You must be signed in to change notification settings - Fork 604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make use of Graph<T>
/ Node<T>
types?
#435
Comments
/
Node<T>` types?Graph<T>
/ Node<T>
types?
Same question here. |
import { dagre } from 'dagre-d3';
const g = dagre.graphlib.Graph<Your type here>(opt) |
This is not answering the question at all. |
Looking at the type definitions again, the answer is this: Line 132 in c1cf91c
Which means that we can do things like: type A = {foo: string};
const g: Graph<T> = ...
const n: Node<T> = g.node("node-id");
n.foo = "bar"; // type-safe, known to be a string attribute |
To be fair, this is only type-safe if T has only optional attributes, because: type A = {foo: string};
const g: Graph<T> = ...
const n: Node<T> = g.node("node-id");
console.log(n.foo); // undefined, but should be string |
This generic type Line 35 in 2216b81
and Line 43 in 2216b81
come from the class definition: Line 11 in 2216b81
So you need to provide the type when initializing new graph:
We can then infer the type from those function. Note that we don't need to declare the type for type A = {foo: string};
const g = dagre.graphlib.Graph<A>(opt)
const n = g.node("node-id");
console.log(n.foo); // type of n.foo is string You also can provide the type for const n: Node<A> = {...} This is a very basic typescript syntax, not related to dagrejs at all. |
Once again you fail to understand the question. We are not asking about the syntax of the generic parameter but about how to make use of it in this context, since it does not appear in any method signature. See my comment above which answers the question for me and hopefully also for @voxpelli. |
@dagrejs/dagre
exposes eg.Graph<T>
,Node<T>
types while@dagrejs/graphlib
doesn't, but it's not clear how to use these to provide extended values on a node in the graph.The node is returned using the extended
Node<T>
:dagre/index.d.ts
Line 35 in 2216b81
But there is no way of assigning any
T
data to it in the first place?dagre/index.d.ts
Line 43 in 2216b81
How are these meant to be used? Considering that the plain types from
@dagrejs/graphlib
are not used there is a use case I assume?The text was updated successfully, but these errors were encountered: