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

sprintf-typescript

v1.0.2

Published

Modern TypeScript sprintf/vsprintf. Drop-in replacement for sprintf-js with template-literal-typed format strings.

Readme

sprintf-typescript

A modern, fully-typed TypeScript reimplementation of sprintf-js. It is a drop-in replacement for sprintf-js — same functions, same format specifiers, same semantics — but rewritten from scratch with:

  • Template-literal-typed format strings. The compiler parses your format string and infers argument types, and where possible the return type too.
  • ESM-only. No CommonJS, no UMD, no AMD, no browser globals.
  • Zero runtime dependencies.
  • Modern tooling. Vite, Vitest, oxlint, TypeScript ≥ 5.6, Node.js ≥ 20.

Install

npm install sprintf-typescript

Usage

import { sprintf, vsprintf } from 'sprintf-typescript';

sprintf('Hello, %s!', 'world');
// ⇒ "Hello, world!"   (inferred return type: "Hello, world!")

sprintf('%d items at %.2f each', 3, 9.99);
// ⇒ "3 items at 9.99 each"

sprintf('%(user.name)s has %(user.posts.length)d posts', {
  user: { name: 'Dolly', posts: [/* … */] },
});

vsprintf('%2$s %3$s a %1$s', ['cracker', 'Polly', 'wants']);
// ⇒ "Polly wants a cracker"

If a %d is given a non-number, or a format string is malformed, you get a TypeError / SyntaxError at runtime — and likely a type error at compile time too.

Format grammar

%[index$|(name)][flags][width][.precision]specifier

Specifiers

| Specifier | Output | | --------: | ------------------------------------------ | | % | A literal % | | b | Binary integer | | c | Character (from char code) | | d i | Signed decimal integer | | e | Scientific notation | | f | Fixed-point float | | g | Float (general, uses toPrecision) | | j | JSON-serialised value (width → indent) | | o | Unsigned octal | | s | String (anything coerced via String(...)) | | t | Boolean ("true" / "false") | | T | Type name (e.g. "number", "array") | | u | Unsigned decimal | | v | Primitive via .valueOf() | | x | Lowercase hexadecimal | | X | Uppercase hexadecimal |

Flags

| Flag | Meaning | | ---------- | -------------------------------------------------- | | + | Always emit a sign for numeric specifiers | | - | Left-align within the width | | 0 | Pad numeric output with zeros | | '<char>' | Pad with <char> (any single character after ') |

Named arguments

sprintf('%(path.to[0].key)s', { path: { to: [{ key: 'hi' }] } });

Paths support .key and [index] access, arbitrarily nested.

Function-valued arguments

If an argument is a function, it is called with no arguments and its return value is used — except for %T and %v, which expect the function value itself.

TypeScript story

The generic overloads of sprintf / vsprintf parse the format string at the type level, so:

sprintf('%d', 'oops');
//              ~~~~~ Argument of type 'string' is not assignable to parameter of type 'number'.

sprintf('%s %s', 'only one');
//       ^^^^^^ Expected 3 arguments, but got 2.

const s = sprintf('Hi, %s!', 'world');
//    ^? const s: "Hi, world!"

For format strings with width/precision/padding, the compile-time result gracefully falls back to string for that slot while keeping the literal parts literal.

Credits

sprintf itself is a POSIX/C standard library function, and most of the format grammar here (specifiers, flags, width, precision, %n$ positional arguments) comes from C. The %(name)s named-argument syntax follows Python's convention.

This package is a from-scratch reimplementation that aims to be a drop-in replacement for sprintf-js by Alexandru Mărășteanu — matching its JS-specific extensions (%j, %T, %v, %t) and coercion semantics.

License

MIT — see LICENSE.