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-strategy

v1.0.0

Published

Type-safe strategy pattern for TypeScript. Register named strategy variants and execute them with compile-time type safety.

Readme

ts-strategy

Type-safe strategy pattern for TypeScript. Register named strategy variants and execute them with compile-time type safety.

Install

npm install ts-strategy

Requires TypeScript 5+.

Usage

import { createStrategy } from 'ts-strategy';

const pricing = createStrategy(
  { variant: 'flat', toExecute: (amount: number) => amount },
  { variant: 'percentage', toExecute: (amount: number) => amount * 0.1 }
);

pricing.variant('flat').execute(100); // 100
pricing.variant('percentage').execute(100); // 10

API

createStrategy(...config)

function createStrategy<VariantId extends string, StrategyFn>(
  ...config: { variant: VariantId | VariantId[]; toExecute: StrategyFn }[]
): {
  variant: (id: VariantId) => { execute: (...args) => ReturnType<StrategyFn> };
};

Each config object maps one or more variant IDs to a toExecute function. Returns a strategy object with a .variant(id).execute(...args) chain.

Parameters

  • config — Rest arguments. Each is an object with:
    • variant — A string or array of strings identifying this variant.
    • toExecute — The function to run when this variant is selected.

Returns an object with:

  • .variant(id) — Selects a variant by ID. Returns an object with .execute().
  • .execute(...args) — Calls the variant's toExecute function with the provided arguments and returns its result.

Type safety

All toExecute functions must have compatible signatures. TypeScript infers a single shared function type from all config entries:

// This compiles — both functions take (name: string) and return string
const strategy = createStrategy(
  { variant: 'a', toExecute: (name: string) => `Hello ${name}` },
  { variant: 'b', toExecute: (name: string) => `Goodbye ${name}` }
);

// This does NOT compile — (string) => string and (number) => number are incompatible
const strategy = createStrategy(
  { variant: 'a', toExecute: (x: string) => x },
  { variant: 'b', toExecute: (x: number) => x } // Type error
);

Variant IDs are narrowed to string literals. Passing an unregistered ID to .variant() is a compile-time error:

const strategy = createStrategy(
  { variant: 'a', toExecute: () => 1 },
  { variant: 'b', toExecute: () => 2 }
);

strategy.variant('a'); // OK
strategy.variant('c'); // Type error: '"c"' is not assignable to '"a" | "b"'

Array variants

Map multiple variant IDs to the same function:

const strategy = createStrategy(
  { variant: 'a', toExecute: () => 'solo' },
  { variant: ['b', 'c'], toExecute: () => 'shared' }
);

strategy.variant('b').execute(); // 'shared'
strategy.variant('c').execute(); // 'shared'

Error handling

Errors are deferred to .execute(). Calling .variant(id) with an unregistered ID does not throw — the error surfaces when .execute() is called:

// At runtime (e.g. via type assertion or dynamic input):
strategy.variant('unknown' as any).execute();
// throws Error: "No function defined for variant unknown"

Duplicate variant IDs

If the same variant ID appears in multiple config entries, the last one wins:

const strategy = createStrategy(
  { variant: 'a', toExecute: () => 'first' },
  { variant: 'a', toExecute: () => 'second' }
);

strategy.variant('a').execute(); // 'second'

License

MIT