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

@ttsc/factory

v0.16.6

Published

Self-contained legacy-style TypeScript AST factory and printer for source code generation, surviving the TypeScript-Go (tsgo) migration. Zero dependencies, no `typescript` import.

Downloads

3,806

Readme

@ttsc/factory

NPM Version NPM Downloads GitHub License Build Status

Hand-written, dependency-free TypeScript AST factory and printer for source code generation.

npm install @ttsc/factory
import factory, { TsPrinter } from "@ttsc/factory";

const node = factory.createCallExpression(
  factory.createPropertyAccessExpression(
    factory.createIdentifier("console"),
    factory.createIdentifier("log"),
  ),
  undefined,
  [factory.createStringLiteral("hello world")],
);

const printer = new TsPrinter();
console.log(printer.print(node));
// console.log("hello world")

Why?

The legacy (<= 6.x, JavaScript based) TypeScript compiler exposes a node factory and a printer through its JavaScript API:

import ts from "typescript";

const node = ts.factory.createStringLiteral("hello");
const text = ts.createPrinter().printNode(/* ... */);

Once a project migrates its tool-chain to the TypeScript-Go (tsgo, >= 7.x) native compiler, that JavaScript ts.factory / ts.Printer API is gone — so AST based code generation built on top of it breaks.

@ttsc/factory keeps that capability alive without importing typescript at all. The factory and printer are re-implemented directly, so the package has zero dependencies and works no matter which compiler builds the rest of your project.

API

| Export | Description | | --- | --- | | factory (default export) | The node factory; createXxx mirror the legacy signatures. | | TsPrinter | Renders factory nodes to TypeScript source text. | | SyntaxKind, NodeFlags | Outline token & flag enums. | | addSyntheticLeadingComment | Attach // / /* */ comments to a node. | | Outline AST types | Expression, Statement, TypeNode, Node, ... |

factory

createXxx methods mirror the legacy ts.factory names and parameter order, and return concrete, fully typed outline AST nodes (each with a kind discriminant).

import factory, { SyntaxKind } from "@ttsc/factory";

factory.createKeywordTypeNode(SyntaxKind.StringKeyword); // string

TsPrinter

A width-aware printer implemented directly (not a wrapper over ts.Printer). Like Prettier, it keeps lists on one line when they fit within printWidth and breaks them — with trailing commas — when they don't.

const props: TsPrinter.IProps = {
  printWidth: 80, // default 80
  indent: "  ", //   default two spaces
  newLine: "\n", //  default LineFeed
};
const printer = new TsPrinter(props);

printer.print(node); // print one node (or a SourceFile)
printer.printNodes([a, b, c]); // print many nodes, joined by new lines
printer.printFile(undefined, st); // compose & print a whole source file
// fits on one line → inline
factory.createCallExpression(id("foo"), undefined, [a, b]); // foo(a, b)

// exceeds printWidth → breaks
// foo(
//   argumentOne,
//   argumentTwo,
//   argumentThree,
// )

Comments

Attach leading / trailing comments with the legacy ts.addSyntheticLeadingComment family. The printer renders them in place — multi-line bodies re-indent with their node, so JSDoc on a nested member stays aligned.

import factory, {
  SyntaxKind,
  TsPrinter,
  addSyntheticLeadingComment,
} from "@ttsc/factory";

const node = addSyntheticLeadingComment(
  factory.createTypeAliasDeclaration(
    undefined,
    "ID",
    undefined,
    factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
  ),
  SyntaxKind.MultiLineCommentTrivia,
  "*\n * The identifier.\n ",
  true,
);

new TsPrinter().print(node);
// /**
//  * The identifier.
//  */
// type ID = string;

Companion helpers: addSyntheticTrailingComment, get/setSyntheticLeadingComments, get/setSyntheticTrailingComments.

Coverage

The factory and printer cover the constructs most used for code generation: identifiers, literals, the common expressions, types (keyword / reference / union / intersection / array / tuple / type-literal / function / operator / ...), statements, classes & interfaces, enums, functions & arrow functions, and import / export declarations. Coverage is easy to extend — add the node under src/ast/, a builder under src/factory/, and a case to the printer.

License

MIT © Jeongho Nam