tomwanzek / d3-v4-definitelytyped

[DEPRECATED] This repo was intended as a staging area for typescript definitions supporting the latest major release of D3js (i.e. version 4.1.x) by Mike Bostock. It has been migrated to DefinitelyTyped.
MIT License
53 stars 14 forks source link

How to use SimulationLinkDatum and SimulationNodeDatum #136

Closed dcavanagh closed 7 years ago

dcavanagh commented 7 years ago

I'm having a problem using the SimulationLinkDatum Type. I made two classes Node and Link that implement SimulationNodeDatum and SimulationLinkDatum. When I go to try and use SimulationLinkDatum typescript says that d.source.x, d.targe.x, etc typescript says that "Property 'x' does not exist on type 'string | number | Node'."

I understand that the SimulationLinkDatum accepts number | string | <T> for the source and target node properties and that if you use number or string that it will be mutated into a SimulationNodeDatum. Is this just something that we have to live with or is there a better way of using these interfaces?

Thanks


class D3Component {
    private createPathString(d: SimulationLinkDatum<Node>) {
        return 'M' + d.source.x + ',' + d.source.y + 'L' + d.target.x + ',' + d.target.y;
    }
}
class Node implements SimulationNodeDatum {
    public x: number;
    public y: number;
    constructor (public id: number) {}
}

class Link implements SimulationLinkDatum<Node> {
    constructor (public source: Node, public target: Node)  {}
}```
tomwanzek commented 7 years ago

As your question is more of a "how to" question, please post it on StackOverflow. Also, because I think it might be of broader interest, when answered there. You can ping me there or drop me a link to your SO question in this thread for reference.

Nevertheless, I am closing this issue, as this repo is not active for issues, but I am more than willing to respond as proposed above. Thx, for understanding, T.

dcavanagh commented 7 years ago

Thanks - sorry about that. Here is a link to the SO question http://stackoverflow.com/questions/43696302/how-to-use-simulationlinkdatum-and-simulationnodedatum-in-d3

tomwanzek commented 7 years ago

Thanks for opening the SO question. I just posted a suggested answer.