Graphology TypeScript Types

0.24.8 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to define and use a `graphology` graph with custom type-safe node and edge attributes using `graphology-types`.

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}`);

view raw JSON →