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

v1.0.0

Published

A type-safe, React reducer hook for scalable state management

Readme

ts-yreducer

npm version MIT License TypeScript React

A type-safe, React reducer hook for scalable state management


Features

  • 🛡️ Type safety: Strict types for state and actions, reducing bugs.
  • 🧩 Familiar API: Works like React’s useReducer, but with better type inference.
  • 🚀 Scalable: Perfect for complex state logic in large React apps.
  • Lightweight: Zero dependencies.

Installation

npm install ts-yreducer
# or
yarn add ts-yreducer

Usage

📦 Example: Counter

import useYReducer from "ts-yreducer";

type CounterState = {
  count: number;
}

type CounterAction =
  | { key: "increment"; payload: number }
  | { key: "decrement"; payload: number }
  | { key: "reset" };

const counterReducer = (state: CounterState, action: CounterAction): CounterState => {
  const { key, payload } = action;
  switch (action.key) {
    case "increment":
      if(!payload) return state; 
      return { count: state.count + action.payload };
    case "decrement":
      if(!payload) return state;   
      return { count: state.count - action.payload };
    case "reset":
      return { count: 0 };
    default:
      return state;
  }
};

function CounterComponent() {
  const [state, dispatch] = useYReducer({ count: 0 }, counterReducer);

  return (
    <div>
      <p>Count: {state.count}</p>
      <button onClick={() => dispatch({ key: "increment", payload: 1 })}>+</button>
      <button onClick={() => dispatch({ key: "decrement", payload: 1 })}>-</button>
      <button onClick={() => dispatch({ key: "reset" })}>Reset</button>
    </div>
  );
}

📦 Example: Todo List

import useYReducer, { type ActionObject, type ReducerState, type ReducerFunc }  from "ts-yreducer";

type StateKey = "add" | "remove" | "setFilter";
type FilterKey = "all" | "completed" | "active"; 

type StateUnit<TKey extends StateKey, TPayload = unknown> = ActionObject<
  TKey,
  TPayload
>;

interface TodoState extends ReducerState {
  todos: string[];
  filter: FilterKey;
}

type TodoActions =
  | StateAction<"add", string>
  | StateAction<"remove", number>
  | StateAction<"setFilter", FilterKey>;

const reducer:ReducerFunc<TodoState, TodoActions> = (state: TodoState, action: TodoActions) => {
  const { key, payload } = action;

  switch (key) {
    case "add":
      if (!payload) throw new Error("Todo is required"); 
      return { ...state, todos: [...state.todos, payload] };

    case "remove":
      if (!payload) throw new Error("Index is required"); 
      return {
        ...state,
        todos: state.todos.filter((_, idx) => idx !== payload),
      };

    case "setFilter":
      if (!payload) throw new Error("Filter key is required");  
      return { ...state, filter: payload };

    default:
      return state;
  }
};

function TodoComponent() {

  const initialStates: TodoState = {todos: [], filter: "all"};
  const [state, dispatch] = useYReducer(initialStates,reducer);

  return (
    <div>
      <button onClick={() => dispatch({ key: "add", payload: "Learn TypeScript" })}>
        Add Todo
      </button>
      <ul>
        {state.todos.map((todo, idx) => (
          <li key={idx}>
            {todo}
            <button onClick={() => dispatch({ key: "remove", payload: idx })}>Remove</button>
          </li>
        ))}
      </ul>
      <div>
        <button onClick={() => dispatch({ key: "setFilter", payload: "all" })}>All</button>
        <button onClick={() => dispatch({ key: "setFilter", payload: "active" })}>Active</button>
        <button onClick={() => dispatch({ key: "setFilter", payload: "completed" })}>Completed</button>
      </div>
      <p>Current filter: {state.filter}</p>
    </div>
  );
}

API

useYReducer<TState, TAction>(initialState, reducer)

  • TState: The shape of your state object.
  • TAction: The union of possible action objects.
  • Returns: [state, dispatch]

Types

/**
 * Represents the shape of the reducer state.
 * Extend this interface to define your own state structure.
 */
export interface ReducerState {
  [key: string]: unknown;
}

/**
 * Represents a single action object for the reducer.
 *
 * @template TKey - The action type identifier.
 * @template TPayload - The payload type for the action.
 */
export interface ActionObject<TKey extends string, TPayload = unknown> {
  /**
   * Action identifier
   */
  readonly key: TKey;

  /**
   * Action payload.
   */
  readonly payload?: TPayload;
}

/**
 * Type alias for a union of all possible action objects.
 */
export type ActionUnion = ActionObject<string, unknown>;

/**
 * Reducer function signature.
 *
 * @template TState - The state type.
 * @template TAction - The action union type.
 */
export type ReducerFunc<
  TState extends ReducerState,
  TAction extends ActionUnion
> = (state: TState, unit: TAction) => TState;

/**
 * Dispatch function signature for actions.
 *
 * @template TAction - The action union type.
 *
 * @param unit - The action to be dispatched.
 */
export type DispatchFunc<TAction extends ActionUnion> = (unit: TAction) => void;

/**
 * The return type of the useYReducer hook: [state, dispatch].
 *
 * @template TState - The state type.
 * @template TAction - The action union type.
 */
export type UseYReducerReturn<
  TState extends ReducerState,
  TAction extends ActionUnion
> = [TState, DispatchFunc<TAction>];

License MIT


Type-safe reducers, made easy.