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

typedswitch

v1.0.0

Published

A type-safe, exhaustive switch expression for TypeScript discriminated unions and string literals

Readme

typedswitch

A type-safe, exhaustive switch expression for TypeScript. Handle discriminated unions and string literals with full type inference, compile-time exhaustiveness checking, and async support.

Installation

npm install typedswitch
pnpm add typedswitch
bun add typedswitch

Features

  • Full type inference — Return types are automatically inferred from your handlers
  • Exhaustiveness checking — TypeScript ensures all cases are handled at compile time
  • Discriminated union support — Works with any discriminant key, not just type
  • Async-aware — Mixed sync/async handlers return Promise automatically
  • Default handlers — Handle remaining cases with a fallback
  • Return type constraints — Enforce that all handlers return a specific type

Usage

String/Enum Input

Switch on a string literal or enum value directly:

import { typedSwitch } from 'typedswitch'

type Status = 'success' | 'error' | 'pending'

const status: Status = 'success'

// All cases required — TypeScript enforces exhaustiveness
const message = typedSwitch(status, {
  success: () => 'Operation completed!',
  error: () => 'Something went wrong',
  pending: () => 'Please wait...',
})
// message: string

Each handler receives the narrowed value:

const result = typedSwitch(status, {
  success: (val) => val.toUpperCase(), // val: 'success'
  error: (val) => val.toUpperCase(),   // val: 'error'
  pending: (val) => val.toUpperCase(), // val: 'pending'
})

Discriminated Union Input

Switch on objects with a discriminant property (like type, kind, status, etc.):

type Event =
  | { type: 'click'; x: number; y: number }
  | { type: 'scroll'; offset: number }
  | { type: 'keypress'; key: string }

const event: Event = { type: 'click', x: 100, y: 200 }

// Specify the discriminant key as the second argument
const description = typedSwitch(event, 'type', {
  click: (e) => `Clicked at (${e.x}, ${e.y})`,   // e: { type: 'click'; x: number; y: number }
  scroll: (e) => `Scrolled ${e.offset}px`,       // e: { type: 'scroll'; offset: number }
  keypress: (e) => `Pressed ${e.key}`,           // e: { type: 'keypress'; key: string }
})
// description: string

Works with any discriminant key:

type Order =
  | { status: 'pending'; createdAt: Date }
  | { status: 'completed'; completedAt: Date }
  | { status: 'cancelled'; reason: string }

const order: Order = { status: 'completed', completedAt: new Date() }

const info = typedSwitch(order, 'status', {
  pending: (o) => `Created: ${o.createdAt}`,
  completed: (o) => `Done: ${o.completedAt}`,
  cancelled: (o) => `Cancelled: ${o.reason}`,
})

Default Handler

Handle a subset of cases and catch the rest with a default handler:

type Status = 'success' | 'error' | 'pending' | 'cancelled'

const status: Status = 'cancelled'

// Only handle specific cases, default handles the rest
const message = typedSwitch(
  status,
  {
    success: () => 'Done!',
    error: () => 'Failed!',
  },
  (val) => `Unhandled status: ${val}`  // val: Status (full union)
)
// message: string

Works with discriminated unions too:

const description = typedSwitch(
  event,
  'type',
  {
    click: (e) => `Clicked at (${e.x}, ${e.y})`,
  },
  (e) => `Unhandled event: ${e.type}`  // e: Event (full union)
)

Async Handlers

Mix sync and async handlers freely. If any handler returns a Promise, the result is automatically typed as Promise:

// All sync — returns string
const syncResult = typedSwitch(status, {
  success: () => 'ok',
  error: () => 'err',
  pending: () => 'wait',
})
// syncResult: string

// Any async — returns Promise<string>
const asyncResult = typedSwitch(status, {
  success: async () => {
    const data = await fetchData()
    return data.message
  },
  error: () => 'err',           // sync handlers still work
  pending: async () => 'wait',
})
// asyncResult: Promise<string>

const message = await asyncResult

Return Type Inference

Return types are inferred from all handlers as a union:

const result = typedSwitch(status, {
  success: () => ({ ok: true, data: 'hello' }),
  error: () => ({ ok: false, code: 500 }),
  pending: () => null,
})
// result: { ok: true; data: string } | { ok: false; code: number } | null

Return Type Constraints

Enforce that all handlers return a type extending a constraint using the curried form typedSwitch<Constraint>():

interface HasId {
  id: string
}

// All handlers must return something with { id: string }
const result = typedSwitch<HasId>()(status, {
  success: () => ({ id: '123', name: 'John' }),     // ✓ OK
  error: () => ({ id: '456', code: 500 }),          // ✓ OK
  pending: () => ({ id: '789' }),                   // ✓ OK
})
// result: { id: string; name: string } | { id: string; code: number } | { id: string }

// This would be a compile error:
typedSwitch<HasId>()(status, {
  success: () => ({ name: 'John' }),  // ✗ Error: missing 'id'
  error: () => ({ id: '456' }),
  pending: () => ({ id: '789' }),
})

Works with discriminated unions and default handlers:

const result = typedSwitch<HasId>()(
  event,
  'type',
  {
    click: () => ({ id: 'click-1', x: 10, y: 20 }),
    scroll: () => ({ id: 'scroll-1', offset: 100 }),
    keypress: () => ({ id: 'key-1', key: 'Enter' }),
  }
)

Error Handling

When a case is unhandled at runtime (e.g., due to unsafe casts), typedSwitch throws a descriptive error:

const status = 'unknown' as Status

typedSwitch(status, {
  success: () => 'ok',
  error: () => 'err',
})
// Throws: "Unhandled case: unknown. Available cases: success, error"

For discriminated unions:

// Throws: "Unhandled case: unknown (discriminant key: "type"). Available cases: click, scroll, keypress"

API Reference

typedSwitch(value, cases)

Switch on a string literal with exhaustive case handling.

typedSwitch(value, cases, defaultHandler)

Switch on a string literal with partial cases and a default fallback.

typedSwitch(object, key, cases)

Switch on a discriminated union using the specified discriminant key.

typedSwitch(object, key, cases, defaultHandler)

Switch on a discriminated union with partial cases and a default fallback.

typedSwitch<Constraint>()

Returns a constrained version of typedSwitch that enforces all handlers return a type extending Constraint.

License

MIT