mxGraph usage in TypeScript projects
Tags: mxgraph, typescript
Published: 2021-06-01 10:19:05

This article is the first one of a series about [mxGraph](https://github.com/jgraph/mxgraph), the Javascript diagramming library.

Over the last few months, my team has used it on a day-to-day basis as the rendering foundation of [bpmn-visualization](https://github.com/process-analytics/bpmn-visualization-js), an open source TypeScript  library for visualizing process execution data on [BPMN](https://www.omg.org/spec/BPMN/2.0.2/) diagrams.

In this series, I will share integration challenges and solutions when using mxGraph in TypeScript projects, tooling developed to simplify mxGraph based developments, rendering extensions, usage tips and much more.

Most aspects described in the articles will also apply to JavaScript projects using mxGraph.



mxGraph and TypeScript: is it possible?
---------------------------------------

A few years ago, the mxGraph core development team stated that it would never port mxGraph to TypeScript, nor support it, and this was still applicable in 2020. (See [issue \#81](https://github.com/jgraph/mxgraph/issues/81) and related issues for more.)

If it is not supported, why would you even want to use mxGraph in a TypeScript project? Well, if your application integrates Angular or React, you probably already use TypeScript, so if you want to add mxGraph, a TypeScript integration would be nice.

You might also find it useful in vanilla TypeScript applications, or libraries like [bpmn-visualization](https://github.com/process-analytics/bpmn-visualization-js). When my team started our development, we wanted to build a TypeScript library that uses mxGraph for rendering.

In this article, I’ll explain how we handled this, the issues we faced, and how we have successfully integrated mxGraph.


![Cactus](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wripjyd6ujujy4eu2c0c.png)
*“Cactus” by [Steven Ritts](https://www.freeimages.com/photo/cactus-1392156)*



Plenty of disappointing examples and solutions
----------------------------------------------

We started out as most developers do: when we saw that mxGraph has no native TypeScript support, we went to check how other people might have tried to do it.

However, we saw - almost always - the same kinds of custom, hacky, and disappointing solutions. Let’s look at some of them and why we thought they are a pain.

### The ‘any’ way

No mxGraph native Typescript support? No problem, let’s use any everywhere!

```typescript
declare var mxGraph: any;

const graph = new mxGraph(this.graphContainer.nativeElement);
```

Using ***any*** is fast, but be ready for no code assistance, no TypeScript compiler checks. In short, this is not the TypeScript way you expect. And remember, this is [something the TypeScript documentation recommends not to do](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#any).

You may also use unknown which is better for the TypeScript compiler, but it won't help much more than any.

Examples:

-   [<u>https://therichpost.com/angular-8-mxgraph-working-example/</u>](https://therichpost.com/angular-8-mxgraph-working-example/)
-   [<u>https://stackoverflow.com/questions/63495464/error-during-mxgraph-integration-with-angular-10</u>](https://stackoverflow.com/questions/63495464/error-during-mxgraph-integration-with-angular-10)



### The ‘define your own mxGraph types’ way

Define your own mxGraph types for only the parts of the lib you are using.

This is only viable if you use a very small part of mxGraph. It is a large library, and as you use more and more features, the number of types you have to maintain increases.

Example: [<u>https://github.com/diegogusava/angular-mxgraph/tree/fc284593b09c4db8c355d663d1189ecdb369b26c/src/mxtypes</u>](https://github.com/diegogusava/angular-mxgraph/tree/fc284593b09c4db8c355d663d1189ecdb369b26c/src/mxtypes)



### The ‘copy unmaintained types’ way

Retrieve the work done by a single individual who has shared their attempt on mxGraph types, and copy everything directly in your project.

Be ready for a maintenance nightmare:

-   the types may be out of date, full of any instead of actual types, so you will find yourself modifying the types anyway
    
-   as mxGraph evolves, you will also have to update the types to reflect API changes
    
-   and...you will work alone on this tedious task.

This is a lot of extra work, and probably not what you expect from a true integration.

Examples:

-   [<u>https://medium.com/habilelabs/how-to-integrate-mx-graph-with-angular-8-b4a8bb800694</u>](https://medium.com/habilelabs/how-to-integrate-mx-graph-with-angular-8-b4a8bb800694)
-   [<u>https://itnext.io/how-to-integrate-mxgraph-with-angular-6-18c3a2bb8566</u>](https://itnext.io/how-to-integrate-mxgraph-with-angular-6-18c3a2bb8566)


![Road Between Pine Trees](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ped56u3lvp0qsvv12tw.png)
*“Road Between Pine Trees” by [<u>veeterzy</u>](https://www.pexels.com/photo/road-between-pine-trees-39811/)*



Searching for true mxgraph types
--------------------------------

mxGraph is not the sole JavaScript library that hasn’t TypeScript native support.

In such situations, [<u>DefinitelyTyped</u>](https://definitelytyped.org/) is the way to go. It is a community-led open-source repository for TypeScript type
definitions.

Using types from DefinitelyTyped is very easy. Once you add the types package as a dependency of your project, TypeScript is automatically
able to read the types without any extra configuration. Unfortunately, although there is a [<u>request for adding mxGraph types to DefinitelyTyped</u>](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/5317), we found that there was no one working on it in 2020.

We also found some individual efforts to make mxGraph TypeScript usage easier. But everyone appeared to be working individually on their own  solutions, leading to the following issues:

-   Most repository owners said that the type definition files in their project were not complete (or something similar). mxGraph provides a large API, so it is hard to keep types in sync as the lib evolves.
    
-   It is almost impossible to know which mxGraph version was targeted by the various type definition packages.

As we found several solutions and [<u>npm packages</u>](https://www.npmjs.com/search?q=mxgraph%20typescript), so it was hard to find the best way to use mxGraph in a TypeScript project.

mxGraph is a very large library, which may frighten people about starting extracting and maintaining types.

If you are interested in more information, you can check this [<u>non-exhaustive list</u>](https://github.com/process-analytics/mxgraph-road-to-DefinitelyTyped/blob/master/existing-solutions.md) (last updated in mid-2020) that presents various solutions with
information about their activity status and implementation choices.


![Beautiful sunlight through clouds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/si9cho9kjl94qopy1kjg.png)
*“Beautiful sunlight through clouds” by [<u>Yury Khristich</u>](https://www.freeimages.com/photo/beautiful-sunlight-through-clouds-1171178)*



The 2021 solution
-----------------

In 2020, a community effort emerged to make mxGraph types available in a consistent way, and a viable solution is now available.

This first started as [<u>mxgraph-type-definitions</u>](https://github.com/hungtcs/mxgraph-type-definitions). Most parts of mxGraph were covered progressively, and types became more
accurate thanks to user feedback. Projects like [<u>grafana-flowcharting</u>](https://github.com/algenty/grafana-flowcharting/blob/0.9.0/package.json#L70) began using it.

Because it had issues integrating with the mxGraph npm package, it evolved into [<u>typed-mxgraph</u>](https://github.com/typed-mxgraph/typed-mxgraph) in Fall 2020, and mxgraph-type-definitions was finally deprecated in December 2020.

This effort is still led by [<u>@hungtcs</u>](https://github.com/hungtcs), the original creator of the *mxgraph-type-definitions* repository.

Here is an example of what you can write with typed-mxgraph:

```typescript
class Application {
  constructor(container: HTMLElement) {
    const graph: mxGraph = new mx.mxGraph(container);
    const model: mxGraphModel = graph.getModel();
    model.beginUpdate();
    try {
      graph.insertVertex(graph.getDefaultParent(), '', 'TEST', 0, 0, 100, 100);
    } finally {
      model.endUpdate();
    }
  }
}
```



Our [<u>bpmn-visualization</u>](https://github.com/process-analytics/bpmn-visualization-js) project has followed a similar evolutionary path to the mxGraph usage in TypeScript projects.

We initially started with our own [<u>ts-mxgraph</u>](https://github.com/process-analytics/ts-mxgraph) fork, and we were forced to manage the types by ourselves. We struggled with out-of-date types and had to manage updates for our sole usage.

When [<u>mxgraph-type-definitions</u>](https://github.com/hungtcs/mxgraph-type-definitions) was mature enough, we used it in our production code. We quickly benefited from fixes and started contributing to improve the types we used in our project.

We still needed ts-mxgraph for our tests due to the existing issue with the mxgraph npm package integration.

And now, we have recently fully switched to typed-mxgraph using examples like the [<u>mxgraph TypeScript example bundled with webpack</u>](https://github.com/typed-mxgraph/typed-mxgraph-example-bundled-with-webpack).

So we have been able to remove our ts-mxgraph fork. We can focus all our mxgraph integration efforts to giving back to the community and getting  further improvements from the community as well.



Take Away
---------

The mxGraph TypeScript integration was difficult and painful in the past, but in 2021 we now have a solution with [<u>typed-mxgraph</u>](https://github.com/typed-mxgraph/typed-mxgraph).
There is no more need for hacky solutions. mxGraph can be integrated into TypeScript projects as with any JavaScript library without native TypeScript support, and it's possible to get code assistance and TypeScript compiler checks.

typed-mxgraph is still a young solution and it will benefit from wider adoption. So, if you are using mxGraph and are using it or want to use it in TypeScript projects, feel free to contribute to [<u>typed-mxgraph</u>](https://github.com/typed-mxgraph/typed-mxgraph).



There are a large number of areas that can be improved:

-   provide usage feedback (missing or incorrect types)

-   improve the types coverage

-   improve usage documentation

-   enrich existing examples (more mxGraph API usage) or provide new ones
    
-   help keeping types up to date as the mxGraph javascript library changes

We welcome your contribution to the typed-mxgraph team!



*This article was originally published at [Dzone](https://dzone.com/articles/mxgraph-usage-in-typescript-projects)*