JavaScript Decorators: Memoize, Log, and Deprecated for WordPress JS

JavaScript decorators in WordPress build tooling: memoize, log, deprecate

Decorators wrap class methods with extra behavior declared next to the method: @memoize caches results, @deprecated warns callers, @logged traces calls. The proposal has been at Stage 3 since 2022 and its final form is stable, but no browser runs decorators natively yet. In WordPress projects they are usable today through the build step, with TypeScript 5 or Babel, and the output is plain JavaScript.

Problem: A block plugin’s data layer has a class with methods that fetch from the REST API. Each method has its own copy of caching code, and renamed methods need hand-written deprecation warnings for third-party code that still calls the old names.

Solution: Write small decorator functions (memoize, deprecated) following the standard decorator signature ( value, context ), apply them to methods with @, and enable the transform in the build: TypeScript 5.0+ without experimentalDecorators, or Babel’s decorators plugin with version: "2023-11" in a @wordpress/scripts setup.

A memoize decorator

// src/decorators.js
export function memoize( method, context ) {
    if ( context.kind !== 'method' ) {
        throw new TypeError( '@memoize works on methods only' );
    }
    const caches = new WeakMap(); // one cache per instance

    return function ( ...args ) {
        let cache = caches.get( this );
        if ( ! cache ) {
            cache = new Map();
            caches.set( this, cache );
        }
        const key = JSON.stringify( args );
        if ( ! cache.has( key ) ) {
            cache.set( key, method.apply( this, args ) );
        }
        return cache.get( key );
    };
}

A method decorator receives the original function and a context object (kind, name, static, private, addInitializer) and returns the replacement function. Here the replacement stores results per instance in a WeakMap, so the cache disappears with the object. For a method that returns a promise, the promise itself is cached, so parallel calls share one request.

A deprecation decorator

export function deprecated( replacement ) {
    return function ( method, context ) {
        const name = String( context.name );
        return function ( ...args ) {
            window.wp?.deprecated?.( `HaApi.${ name }`, { since: '3.0', alternative: replacement, plugin: 'HA Plugin' } );
            return method.apply( this, args );
        };
    };
}

@deprecated( 'getSlots' ) is a decorator factory: a function that returns the decorator. It uses wp.deprecated() from @wordpress/deprecated, which prints the standard WordPress deprecation message once per function and triggers the deprecated hook.

Apply them

// src/api.js
import apiFetch from '@wordpress/api-fetch';
import { memoize, deprecated } from './decorators';

export class HaApi {
    @memoize
    getSlots( date ) {
        return apiFetch( { path: `/ha/v1/slots?date=${ encodeURIComponent( date ) }` } );
    }

    @deprecated( 'getSlots' )
    fetchSlots( date ) {
        return this.getSlots( date );
    }
}

Enable the transform

{
    "presets": [ "@wordpress/babel-preset-default" ],
    "plugins": [ [ "@babel/plugin-proposal-decorators", { "version": "2023-11" } ] ]
}

With a .babelrc like this in the project root, wp-scripts build compiles the decorators into normal function calls. In TypeScript 5.0+, standard decorators work by default; do not set experimentalDecorators, which enables the older, incompatible TypeScript-only decorators that many tutorials still show (with a descriptor argument).

When not to use them

Decorators need a build step and only work on classes. Most WordPress JavaScript (blocks, hooks, data stores) is written with functions, where a plain higher-order function does the same: const getSlots = memoize( rawGetSlots ). Use decorators where the code already uses classes; do not convert function code to classes just for them. For a cache that does not keep objects alive: WeakRef and FinalizationRegistry.

NOTE: The decorators proposal is at Stage 3; no browser ships it natively as of October 2025, so decorator syntax in a file that is not compiled is a syntax error. Code in ES modules loaded directly with wp_enqueue_script_module() cannot use them.