Graphology TypeScript Types
graphology-types provides comprehensive TypeScript declaration files for the `graphology` graph manipulation library. Its current stable version is 0.26.0, released in alignment with `graphology`'s development, ensuring type definitions are up-to-date with the core library's API. This package is crucial for developers building type-safe applications with `graphology` in TypeScript environments, offering static checking for graph structures, node and edge attributes, and various graph operations. As an official types package, it guarantees accurate and reliable type information, distinguishing it from community-maintained declaration files which might fall out of sync with library updates. It helps prevent common runtime errors by catching type mismatches at compile time, enhancing developer experience and code maintainability.
Common errors
-
TS2307: Cannot find module 'graphology-types' or its corresponding type declarations.
cause The `graphology-types` package has not been installed or TypeScript cannot locate its declaration files.fixRun `npm install graphology-types` or `yarn add graphology-types`. Ensure `tsconfig.json` includes `node_modules/@types` in its `typeRoots` or that the package is correctly referenced. -
Property 'xyz' does not exist on type 'NodeAttributes'.
cause Attempting to access a node or edge attribute that has not been explicitly defined in the custom `NodeAttributes` or `EdgeAttributes` interface.fixExtend `NodeAttributes` or `EdgeAttributes` with your custom properties, e.g., `interface MyNodeAttrs extends NodeAttributes { xyz: string; }` and use `Graph<MyNodeAttrs>`. -
Argument of type 'number' is not assignable to parameter of type 'string'.
cause A type mismatch occurs when setting or retrieving an attribute, where the value provided does not match the type defined in the custom `NodeAttributes` or `EdgeAttributes` interface.fixEnsure that the values passed to `addNode`, `setNodeAttribute`, etc., strictly conform to the types defined in your custom attribute interfaces. -
Module 'graphology-types' has no exported member 'X'. Did you mean 'Y'?
cause An incorrect named import was used, or the symbol 'X' does not exist in the `graphology-types` package.fixVerify the exact name of the type or interface you are trying to import from the `graphology-types` documentation or its `d.ts` files. Ensure you are using named imports for interfaces and types.
Warnings
- breaking Version `0.26.0` introduces ESM support across the `graphology` ecosystem. While `graphology-types` primarily provides declaration files, this change in the underlying library may impact how `graphology` is imported and subsequently how types are resolved in mixed CJS/ESM projects.
- breaking The `obliterator` dependency was removed in version `0.26.0`. While this is primarily an internal change, if any type definitions from `obliterator` were inadvertently exposed or relied upon in custom extensions, their removal could lead to type errors.
- breaking Version `0.24.0` removed undocumented methods `#.upgradeToMixed` and `#.upgradeToMulti` from the `Graph` class. Although undocumented, if these methods were used, their removal will result in type and runtime errors.
- breaking Version `0.24.0` also includes 'Dropping some irrelevant arities for edge attribute' setters/getters. This could lead to type mismatches if custom attribute functions or specific parameter counts were expected when interacting with edge attributes.
Install
-
npm install graphology-types -
yarn add graphology-types -
pnpm add graphology-types
Imports
- Graph
import Graph from 'graphology-types';
import { Graph } from 'graphology-types'; - NodeAttributes
import { NodeAttributes } from 'graphology-types'; - EdgeAttributes
import { EdgeAttributes } from 'graphology-types';
Quickstart
import Graph from 'graphology';
import { Graph as GraphType, NodeAttributes, EdgeAttributes } from 'graphology-types';
// Define custom interfaces for node and edge attributes
interface MyNodeAttributes extends NodeAttributes {
name: string;
weight?: number;
}
interface MyEdgeAttributes extends EdgeAttributes {
type: 'friend' | 'family';
since: Date;
}
// Instantiate a graph with specific types for node and edge attributes
const graph: GraphType<MyNodeAttributes, MyEdgeAttributes> = new Graph<MyNodeAttributes, MyEdgeAttributes>();
// Add nodes with type-checked attributes
graph.addNode('john', { name: 'John Doe', weight: 80 });
graph.addNode('jane', { name: 'Jane Smith' });
// Add edges with type-checked attributes
graph.addEdge('john', 'jane', { type: 'friend', since: new Date('2020-01-01') });
// Access attributes with type inference
const johnsName: string = graph.getNodeAttribute('john', 'name');
const edgeType: 'friend' | 'family' = graph.getEdgeAttribute('john', 'jane', 'type');
console.log(`John's name: ${johnsName}`);
console.log(`Relationship type: ${edgeType}`);
console.log(`Graph order: ${graph.order}`);
console.log(`Graph size: ${graph.size}`);