threadedClass

raw JSON →
1.4.0 verified Fri May 01 auth: no javascript

Fork a class instance into a separate thread (Node.js Worker Threads or Web Workers) while preserving TypeScript typings. v1.4.0 is stable with a mature API. Key differentiators: one-liner thread instantiation, automatic method promisification, optional auto-restart on crash, and thread pooling via ThreadedClassManager. Actively maintained with frequent releases, supports Node ≥8 and modern browsers.

error TypeError: threadedClass is not a function
cause Default import used instead of named import.
fix
Use import { threadedClass } from 'threadedclass' or const { threadedClass } = require('threadedclass').
error Cannot find module './professor.js'
cause The compiled JS file path is incorrect or the module is not yet compiled.
fix
Ensure the path points to the .js file after compilation (e.g., ./professor.js). Compile TypeScript first.
error Error: Class constructor must be invoked with 'new'
cause Passing the class object directly instead of the export name string in Node.js.
fix
Use the second argument as a string: threadedClass('./professor.js', 'Professor', ...). In browser, you must pass the class reference.
gotcha The module path must point to the compiled JavaScript file, not the TypeScript source. Using .ts will cause runtime errors.
fix Use the compiled output path (e.g., './professor.js').
deprecated The `threadUsage` option is deprecated. Use `ThreadedClassManager.setThreadUsage()` for thread pool control.
fix Use `ThreadedClassManager.setThreadUsage(0.1)` instead of passing `threadUsage` in config.
breaking In v1.0.0, the `threadedClass` function returned the instance directly (sync). Since v1.1.0 it returns a Promise, breaking old code that did not use await.
fix Add `await` before calling `threadedClass()`.
gotcha Properties of the class are not accessible on the forked instance: only methods are promisified. Accessing a property returns `undefined`.
fix Convert properties to getter methods (e.g., `getName()` instead of `name`).
gotcha In browser environments, you must provide `pathToWorker` in the config, otherwise the worker script won't be found.
fix Set config option `pathToWorker: 'lib/threadedclass-worker.js'`.
npm install threadedclass
yarn add threadedclass
pnpm add threadedclass

Creates a forked instance of Professor class running in a separate thread. Constructor arguments are passed as array; all methods become async.

import { threadedClass } from 'threadedclass';

// Assume ./professor.ts exports class Professor with methods like talkAboutAncientGreece()
const mrSmith = await threadedClass<Professor>(
  './professor.js', // path to compiled JS file
  'Professor',
  ['maths', 'greek'], // constructor args
  { autoRestart: true } // optional config
);

// All methods return promises
const story = await mrSmith.talkAboutAncientGreece();
console.log(story);