npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hubspot/ts-export-types-reader

v0.1.2

Published

Reader utilities for the @hubspot/ts-export-types package

Downloads

155

Readme


Overview

This package provides TypeScript types and utilities for working with the type information generated by @hubspot/ts-export-types. It includes:

  • Type definitions — All ApiNode types representing exports, functions, classes, properties, and more
  • Type guards — Functions for narrowing ApiNode types safely
  • Result utilities — Helpers for looking up exports and referenced types

Installation

npm install @hubspot/ts-export-types-reader

Usage

Working with analyze results

import {
  type AnalyzePackageResult,
  createAnalyzeResultReader,
} from '@hubspot/ts-export-types-reader';

// Load your generated API types (from @hubspot/ts-export-types output)
import apiData from './api/api.json';

const api = createAnalyzeResultReader(apiData as AnalyzePackageResult);

// Find an export by name
const myFunction = api.findExportByName({
  exportPath: '.',
  exportName: 'myFunction',
});

// Find a referenced type by its ID
const myType = api.findReferencedTypeById('MyType:my-package:dist/index.d.ts');

Using type guards

import {
  type ApiNode,
  isFunctionNode,
  isObjectNode,
  isTypeReferenceNode,
  isUnionNode,
} from '@hubspot/ts-export-types-reader';

function processNode(node: ApiNode) {
  if (isFunctionNode(node)) {
    console.log('Function parameters:', node.parameters);
    console.log('Return type:', node.returnType);
  }

  if (isObjectNode(node)) {
    node.properties.forEach((prop) => {
      console.log(`Property: ${prop.name}`);
    });
  }

  if (isUnionNode(node)) {
    node.types.forEach(processNode);
  }

  if (isTypeReferenceNode(node)) {
    console.log('References type:', node.typeId);
  }
}

Traversing types

import {
  type AnalyzePackageResult,
  type ApiNode,
  createAnalyzeResultReader,
  isTypeReferenceNode,
} from '@hubspot/ts-export-types-reader';

function collectAllTypeIds(
  api: ReturnType<typeof createAnalyzeResultReader>,
  node: ApiNode,
  visited = new Set<string>()
): string[] {
  const typeIds: string[] = [];

  if (isTypeReferenceNode(node)) {
    if (!visited.has(node.typeId)) {
      visited.add(node.typeId);
      typeIds.push(node.typeId);

      const referencedType = api.findReferencedTypeById(node.typeId);
      typeIds.push(...collectAllTypeIds(api, referencedType, visited));
    }
  }

  return typeIds;
}

API Reference

Result utilities

createAnalyzeResultReader(result: AnalyzePackageResult): AnalyzeResultExport

Creates a helper object for querying analyze results.

interface AnalyzeResultExport {
  findExportByName(options: FindExportByNameOptions): ExportNode;
  findReferencedTypeById(typeId: string): ApiNode;
}

interface FindExportByNameOptions {
  exportPath: string; // e.g., "." or "./utils"
  exportName: string; // e.g., "myFunction"
}

Types

AnalyzePackageResult

The root type for analyze output:

interface AnalyzePackageResult {
  exports: Record<string, ExportNode[]>;
  types: Record<string, ApiNode>;
}

ApiNode

Union of all possible node types:

type ApiNode =
  | ArrayNode
  | BuiltInTypeReferenceNode
  | ClassNode
  | ConditionalNode
  | ConstructorNode
  | ExportNode
  | ExternalTypeReferenceNode
  | FunctionNode
  | InlinedTypeReferenceNode
  | IntersectionNode
  | LiteralNode
  | MappedTypeNode
  | MethodNode
  | ObjectNode
  | ParameterNode
  | PrimitiveNode
  | PropertyNode
  | TupleElementNode
  | TupleNode
  | TypeParameterNode
  | TypeReferenceNode
  | UnionNode
  | UnknownNode;

ApiNodeKind

Constants for all node kinds:

const ApiNodeKind = {
  Array: 'array',
  BuiltInTypeReference: 'built-in-type-reference',
  Class: 'class',
  Conditional: 'conditional',
  Constructor: 'constructor',
  Export: 'export',
  ExternalTypeReference: 'external-type-reference',
  Function: 'function',
  InlinedTypeReference: 'inlined-type-reference',
  Intersection: 'intersection',
  Literal: 'literal',
  MappedType: 'mapped-type',
  Method: 'method',
  Object: 'object',
  Parameter: 'parameter',
  Primitive: 'primitive',
  Property: 'property',
  Tuple: 'tuple',
  TupleElement: 'tuple-element',
  TypeParameter: 'type-parameter',
  TypeReference: 'type-reference',
  Union: 'union',
  Unknown: 'unknown',
} as const;

Type guards

All type guards follow the pattern is{NodeType}(node: ApiNode): node is {NodeType}:

| Function | Narrows to | | ----------------------------- | --------------------------- | | isArrayNode | ArrayNode | | isBuiltInTypeReferenceNode | BuiltInTypeReferenceNode | | isClassNode | ClassNode | | isConditionalNode | ConditionalNode | | isConstructorNode | ConstructorNode | | isExportNode | ExportNode | | isExternalTypeReferenceNode | ExternalTypeReferenceNode | | isFunctionNode | FunctionNode | | isInlinedTypeReferenceNode | InlinedTypeReferenceNode | | isIntersectionNode | IntersectionNode | | isLiteralNode | LiteralNode | | isMappedTypeNode | MappedTypeNode | | isMethodNode | MethodNode | | isObjectNode | ObjectNode | | isParameterNode | ParameterNode | | isPrimitiveNode | PrimitiveNode | | isPropertyNode | PropertyNode | | isTupleElementNode | TupleElementNode | | isTupleNode | TupleNode | | isTypeParameterNode | TypeParameterNode | | isTypeReferenceNode | TypeReferenceNode | | isUnionNode | UnionNode | | isUnknownNode | UnknownNode |

Node type interfaces

ExportNode

Represents an exported symbol:

interface ExportNode {
  kind: 'export';
  exportName: string;
  declarationKind: ExportDeclarationKind;
  isType?: boolean;
  isDefault?: boolean;
  type: ApiNode;
  jsdoc?: Jsdoc;
}

type ExportDeclarationKind =
  | 'const'
  | 'let'
  | 'var'
  | 'function'
  | 'class'
  | 'type'
  | 'interface'
  | 'enum';

FunctionNode

Represents a function type:

interface FunctionNode {
  kind: 'function';
  functionKind: FunctionKind;
  parameters: ParameterNode[];
  returnType: ApiNode;
  typeParameters?: TypeParameterNode[];
  name?: string;
  isAsync?: boolean;
  jsdoc?: Jsdoc;
}

type FunctionKind = 'arrow' | 'expression' | 'declaration' | 'call-signature';

ClassNode

Represents a class:

interface ClassNode {
  kind: 'class';
  name?: string;
  isAbstract: boolean;
  typeParameters?: TypeParameterNode[];
  implements?: ApiNode[];
  constructors: ConstructorNode[];
  properties: PropertyNode[];
  methods: MethodNode[];
  staticProperties: PropertyNode[];
  staticMethods: MethodNode[];
  jsdoc?: Jsdoc;
}

TypeReferenceNode

References a type defined in the types map:

interface TypeReferenceNode {
  kind: 'type-reference';
  typeId: string;
  typeString: string;
  typeArguments?: ApiNode[];
}

ExternalTypeReferenceNode

References a type from an external package:

interface ExternalTypeReferenceNode {
  kind: 'external-type-reference';
  typeName: string;
  typeString: string;
  packageName: string;
  packageVersion: string;
  typeArguments?: ApiNode[];
}

Requirements

  • Node.js >= 18.0.0

License

MIT © HubSpot