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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@pyts/tsparser

v0.1.0

Published

TypeScript AST parser and code generator from JSON

Readme

@pyts/tsparser

TypeScript AST parser and code generator from JSON.

This library receives a JSON representation of TypeScript AST nodes (generated by pytsast) and converts them to actual TypeScript code.

Installation

npm install @pyts/tsparser

Usage

From JSON String

import { parseAndPrint } from '@pyts/tsparser';

const json = JSON.stringify({
  type: 'factory',
  name: 'createIdentifier',
  args: [{ type: 'literal', value: 'myVariable' }],
});

const code = parseAndPrint(json);
console.log(code); // myVariable

From Serialized Object

import { toTypeScript } from '@pyts/tsparser';

const serialized = {
  type: 'factory',
  name: 'createImportDeclaration',
  args: [
    // ... serialized args from pytsast
  ],
};

const code = toTypeScript(serialized);
console.log(code);

Low-Level API

import { deserialize, printNode } from '@pyts/tsparser';
import ts from 'typescript';

// Deserialize JSON to TypeScript AST
const node = deserialize(serializedJson) as ts.Node;

// Print the node as TypeScript code
const code = printNode(node, { trailingNewline: true });

API

High-Level Functions

  • parse(json: string): ts.Node - Parse JSON string to AST node
  • parseAndPrint(json: string, options?): string - Parse and print as code
  • toTypeScript(node: SerializedNode, options?): string - Convert object to code
  • toTypeScriptFile(nodes: SerializedNode[], options?): string - Multiple nodes to file

Deserializer Functions

  • deserialize(node: SerializedNode): DeserializedValues - Deserialize any node
  • deserializeStatement(node): ts.Statement - Deserialize as statement
  • deserializeStatements(nodes): ts.Statement[] - Deserialize statement array
  • deserializeExpression(node): ts.Expression - Deserialize as expression
  • deserializeDeclaration(node): ts.DeclarationStatement - Deserialize as declaration

Printer Functions

  • printNode(node: ts.Node, options?): string - Print single node
  • printNodes(nodes: ts.Node[], options?): string - Print multiple nodes
  • printFile(statements: ts.Statement[], options?): string - Print as file

Type Guards

  • isLiteral(node): node is SerializedLiteral
  • isNumber(node): node is SerializedNumber
  • isFactory(node): node is SerializedFactory

Serialization Format

The JSON format matches the output from pytsast:

{
  "type": "factory",
  "name": "createIdentifier",
  "args": [
    { "type": "literal", "value": "myVar" }
  ]
}

Node Types

  • literal: { type: "literal", value: null | boolean | string }
  • undefined: { type: "undefined"}
  • number: { type: "number", value: number | string }
  • factory: { type: "factory", name: string, args: SerializedNode[] }

Integration with pytsast

# Python side
>>> from pytsast import factory as ts
>>> import json
>>> 
>>> node = ts.createIdentifier("hello")
>>> json_output = json.dumps(node.serialize().model_dump(), indent=2)
>>> print(json_output)
{
  "type": "factory",
  "name": "createIdentifier",
  "args": [
    {
      "type": "literal",
      "value": "hello"
    }
  ]
}
// TypeScript side
> import { parseAndPrint } from '@pyts/tsparser';
> 
> const jsonFromPython = '...'; // Output from Python
> const tsCode = parseAndPrint(jsonFromPython);
> console.log(tsCode);
hello