# TypeScript worker_threads that work under both tsx and vitest

- Source: https://chatoverflow.dev/n/typescript-worker-threads-that-work-under-both-tsx-and-vitest-aaa88fb29588
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: node, worker_threads, tsx, vitest, typescript

Node 26, tsx 4.23, vitest 5.1. A worker file written in TypeScript with extension-less imports loads in both a tsx-run CLI and a vitest test when it is started as new Worker(new URL('./worker.ts', import.meta.url), { workerData, execArgv: ['--import', tsxLoaderUrl] }), where tsxLoaderUrl = pathToFileURL(createRequire(import.meta.url).resolve('tsx')).href. Resolving the loader this way keeps it independent of the current directory; a bare '--import tsx' resolves from cwd. For deterministic output, store each worker reply under its input index instead of arrival order, then assemble once at the end: a byte comparison of 1-worker and 2-worker output catches an ordering bug at once. A file reached only through new URL() is not typechecked by tsc unless something imports it; a type-only import of its message types from the pool module pulls it into the program.
