Skip to content

Add types using JSDoc to the graphlib file#60

Merged
tbo47 merged 7 commits intotbo47:mainfrom
aloisklink:fix/improve-graphlib-types
Dec 3, 2025
Merged

Add types using JSDoc to the graphlib file#60
tbo47 merged 7 commits intotbo47:mainfrom
aloisklink:fix/improve-graphlib-types

Conversation

@aloisklink
Copy link
Collaborator

Similar to #45, I've gone through the src/graphlib/graph.js file and added proper types to the functions whenever TypeScript couldn't automatically figure out the types.

I've also added type-checking of the *.test.js files to ensure that at least the types match what the unit tests expect!

Stuff I didn't do and why

There's also an MIT-licensed @types/graphlib package that we can copy code from, but I didn't do that since I noticed issues with it that were causing our unit tests to fail type-checking (e.g. not allowing number params for lots of functions, even though the test code uses it a lot). We could copy it over later if we want to have better descriptions for the graphlib functions.

I also tried to apply 8740b83 from #45 and unfortunately it seemed to cause errors in other parts of the code, since they're still not typed properly. But all the graphlib types looked correct to me.

This fixes any potential type errors when calling the `addConflict`
function in our unit tests, after the changes from
dbe53cb (fix: Prototype Pollution (tbo47#54), 2025-10-21).

Fixes: dbe53cb
Vitest adds these automatically, so it didn't cause any issues when
running the tests, but it did cause issues with TypeScript.
This lets TypeScript check that the functions we call on this type are
valid.
Update the `tsconfig.json` file to also type-check the tests, checking
for any potential mistakes we make in TypeScript.

The new `tsconfig.build.json` file is then used for actually building
the types.
I've manually gone through the JSDoc references within the
`src/graphlib/graph.js` file and added JSDoc comments when TypeScript
couldn't automatically find detect the types.

I've tried to keep the types as backwards compatible as possible.

There's also an MIT-licensed [`@types/graphlib`][1] package that we can
copy code from, but I didn't do that since I noticed issues with it that
were causing our unit tests to fail type-checking (e.g. not allowing
`number`s as params for lots of functions, even though the test code
uses it a lot).

[1]: http://npmjs.com/package/@types/graphlib
@aloisklink aloisklink requested a review from tbo47 November 19, 2025 15:33
@tbo47
Copy link
Owner

tbo47 commented Nov 20, 2025

Do you want me to release a new version for it? Or do you want to test more in mermaid first?

@aloisklink
Copy link
Collaborator Author

I've tested it with Mermaid and it looks okay to me!

It even caught some bugs in Mermaid, there was some code calling graphlib with the args in the wrong order, see:

https://github.com/mermaid-js/mermaid/blob/b1fe4ffe97ba74f49c5a3d3058508a3bffd8e0c6/packages/mermaid/src/rendering-util/createGraph.ts#L124-L125

Up to you if you want to make a new release or not 🤷.

This PR is just a types change, so it shouldn't affect any actual code, but it could be worth releasing this with #51.

It's a long weekend in Japan, so if I have free time, I might make another PR trying to add types to the rest of the the src/graphlib folder. But it's sometimes hard to find free time to work on open-source 😅.

Update the Graph documentation with a mixture of:

1. API documentation from the graphlib wiki:
   https://github.com/dagrejs/graphlib/wiki/API-Reference
2. Documentation from
   https://github.com/dagrejs/graphlib/blob/64b31ef3df0270c9bda85b45cb338481ce25618d/lib/graph.js

Both of these are from the graphlib MIT license, which we're
already complying with.
@aloisklink
Copy link
Collaborator Author

I've just pushed another change. I've copied over the official documentation from graphlib with a mixture of:

  1. API documentation from the graphlib wiki: https://github.com/dagrejs/graphlib/wiki/API-Reference
  2. Documentation from https://github.com/dagrejs/graphlib/blob/64b31ef3df0270c9bda85b45cb338481ce25618d/lib/graph.js

Both of these are from the graphlib MIT license, which we're already complying with.

@tbo47 tbo47 merged commit 585dc3f into tbo47:main Dec 3, 2025
2 checks passed
@aloisklink aloisklink deleted the fix/improve-graphlib-types branch December 3, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants