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-fun-decorator

v0.6.1

Published

TypeScript plugin to allow decorator syntax on free functions with runtime wrapping

Readme

ts-fun-decorator

TypeScript plugin to allow decorator syntax on free functions (Python-style), compiled into runtime wrapper calls.

Decorators are treated as runtime wrappers that can change function behavior.

Quickstart

  1. Editor plugin (tsserver)

Add to your tsconfig.json:

{
  "compilerOptions": {
    "plugins": [{ "name": "ts-fun-decorator" }]
  }
}
  1. Build (CLI)
npm run build
node dist/cli.js -p tsconfig.json

Or, when installed as a dependency:

npx fn-tsc -p tsconfig.json
  1. Vite (dev/build)
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { functionDecoratorPlugin } from "ts-fun-decorator/vite";

export default defineConfig({
  plugins: [functionDecoratorPlugin(), react()]
});
  1. Example project

A ready-to-run Vite + React + TSX example lives at:

examples/react-vite

Run it:

cd examples/react-vite
npm install
npm run dev

Migration

  • ts-function-decorator is no longer supported. Update dependencies and imports to ts-fun-decorator.
  • The Vite plugin will warn if it detects the legacy package name in your package.json.
  • CLI config keys accepted: ts-fun-decorator, functionDecorator.

Example

Basic decorator usage:

@log
@memoize(100)
export function add(a: number, b: number) {
  return a + b;
}

@once
const init = () => {
  // ...
};

Emits (conceptually):

export function add(a, b) { /* wrapper */ }
// wrapper lazily initializes: __decorated_add = log(memoize(100)(function (a, b) { return a + b; }));
const init = once(() => { /* ... */ });

Runtime helper API (strong typing, with args/this, return mapping, async chain):

import {
  createDecorator,
  createAsyncDecorator,
  mapReturn,
  mapReturnAsync,
  type DecoratorContext
} from "ts-fun-decorator/runtime";

const log = createDecorator((next, ctx) => {
  console.log("[log]", ctx.name, ctx.args);
  const result = next();
  console.log("[log] =>", result);
  return result;
});

const forceArgs = createDecorator<(a: number, b: number) => number>((next) =>
  next.withArgs(2, 3)
);

const doubleReturn = mapReturn<(value: number) => number, number>((value) => value * 2);

const asyncPlusTen = createAsyncDecorator<
  (a: number, b: number) => Promise<number>,
  number
>(async (next) => {
  const result = await next();
  return result + 10;
});

API docs

Runtime helpers (ts-fun-decorator/runtime)

  • createDecorator(handler):
    • handler(next, ctx) => R
    • next() calls original function
    • next.withArgs(...) calls original with new args
    • next.withThis(thisArg, ...args) calls original with new this/args
  • createAsyncDecorator(handler):
    • handler(next, ctx) => Promise<R>
    • await next() for async chains
  • mapReturn(mapper):
    • maps the original return value
  • mapReturnAsync(mapper):
    • maps an awaited return value
  • DecoratorContext<T>:
    • { name, args, thisArg, original, callOriginal }

Vite plugin (ts-fun-decorator/vite)

functionDecoratorPlugin({
  include?: RegExp | ((id: string) => boolean),
  exclude?: RegExp | ((id: string) => boolean),
  hoistMode?: "lazy" | "eager",
  compilerOptions?: ts.CompilerOptions,
  sourceMap?: boolean
})

CLI config (fn-tsc)

Add to tsconfig.json:

{
  "ts-fun-decorator": {
    "hoistMode": "eager"
  }
}

Hoist mode

  • "lazy" (default): preserves call-before-declaration behavior.
  • "eager": initializes the decorator wrapper immediately; call-before-declaration is not preserved.

Supported targets

  • @decorator above function declarations.
  • @decorator above variable statements with a single declaration whose initializer is an arrow/function expression.

Limitations / Notes

  • Function declarations keep hoisting with lazy mode.
  • One declaration per decorated const/let/var statement.
  • Decorators should be written on their own line immediately above the function/variable.
  • Decorator expressions are parsed as-is; @dec and @dec(...) are supported.
  • This does not validate purity or side effects; it only rewrites syntax.
  • JSX warning: in TSX, only line-start @ (after whitespace) is treated as a decorator. If your JSX text literally starts with @, wrap it like {"@"} or prefix it with other text to avoid being masked.

Project structure

  • src/preprocess.ts: scans and masks function decorators.
  • src/index.ts: tsserver plugin entry.
  • src/transformer.ts: emit-time wrapper transformation.
  • src/cli.ts: build wrapper that preprocesses before TypeScript parses.