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

ts-ast-static-resolver

v0.0.2

Published

A TypeScript utility to convert AST nodes to their literal values. Note: This utility only resolves static values and does not reflect runtime changes.

Readme

TypeScript AST Static Resolver

A TypeScript utility that converts AST nodes to their literal values. This utility is designed to resolve static values from TypeScript AST nodes and does not reflect runtime changes.

Features

  • Resolves various TypeScript AST nodes to their literal values:
    • Numeric literals (including decimals, scientific notation, hex, octal, binary)
    • BigInt literals
    • String literals (including template literals)
    • Regular expression literals
    • Boolean literals (true/false)
    • Undefined values
    • Null values
    • Array literals
    • Object literals
    • Template expressions
    • Variable references
    • Imported constants

Installation

npm install ts-ast-static-resolver

Usage

import { resolveToLiteral } from 'ts-ast-static-resolver';
import ts from 'typescript';

// Create a TypeScript program
const program = ts.createProgram(['your-file.ts'], {
  target: ts.ScriptTarget.Latest,
});

// Get the AST node you want to resolve
const sourceFile = program.getSourceFile('your-file.ts');
const node = // ... get your AST node

// Resolve the node to its literal value
const result = resolveToLiteral(node, program);

// Check the result type and value
if (result.valueType === 'StringLiteral') {
  console.log(result.value); // string value
} else if (result.valueType === 'NumericLiteral') {
  console.log(result.value); // number value
}
// ... handle other types

API

Types

type ResolverResult =
  | { valueType: 'NumericLiteral'; value: number }
  | { valueType: 'BigIntLiteral'; value: bigint }
  | { valueType: 'StringLiteral'; value: string }
  | { valueType: 'RegularExpressionLiteral'; value: RegExp }
  | { valueType: 'TrueKeyword'; value: true }
  | { valueType: 'FalseKeyword'; value: false }
  | { valueType: 'UndefinedKeyword'; value: undefined }
  | { valueType: 'VoidExpression'; value: undefined }
  | { valueType: 'NullKeyword'; value: null }
  | { valueType: 'ArrayLiteralExpression'; value: unknown[] }
  | { valueType: 'ObjectLiteralExpression'; value: Record<string, unknown> }
  | { valueType: undefined; value: undefined };

Functions

resolveToLiteral(expr: ts.Node, program: ts.Program): ResolverResult

Converts a TypeScript AST node to its literal value. If the conversion is not possible, returns { valueType: undefined, value: undefined }.

Examples

Resolving String Literals

const result = resolveToLiteral(stringNode, program);
if (result.valueType === 'StringLiteral') {
  console.log(result.value); // "hello world"
}

Resolving Template Literals

const result = resolveToLiteral(templateNode, program);
if (result.valueType === 'StringLiteral') {
  console.log(result.value); // "Value: 123"
}

Resolving Arrays

const result = resolveToLiteral(arrayNode, program);
if (result.valueType === 'ArrayLiteralExpression') {
  console.log(result.value); // ["hello", 123]
}

Resolving Objects

const result = resolveToLiteral(objectNode, program);
if (result.valueType === 'ObjectLiteralExpression') {
  console.log(result.value); // { hello: 123 }
}

Limitations

  • This utility only resolves static values and does not reflect runtime changes
  • Complex expressions or dynamic values cannot be resolved
  • Some TypeScript-specific features may not be fully supported
  • Certain edge cases and special values may not be resolved as expected

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT