Typescript 中工作线程的编译错误

Compilation error on worker thread in Typescript

此代码来自https://medium.com/@Trott/using-worker-threads-in-node-js-80494136dbb6;
我已经修改它以用 Typescript 编写它,但是我有一个编译错误:

test_workers_numbers.ts:50:18 - error TS2304: Cannot find name 'workerData'.
50 generatePrimes(workerData.start, workerData.range)



有什么问题?

'use strict';
import {Worker, isMainThread, parentPort} from 'worker_threads';
const min = 2;
let primes:number[] = [];

function generatePrimes(start: number, range: number) {
  let isPrime = true;
  let end = start + range;
  for (let i = start; i < end; i++) {
    for (let j = min; j < Math.sqrt(end); j++) {
      if (i !== j && i%j === 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime) {
      primes.push(i);
    }
    isPrime = true;
  }
}

if (isMainThread) {
  const max = 1e7;
  const threadCount = +process.argv[2] || 2;
  const threads = new Set<Worker>();;
  console.log(`Running with ${threadCount} threads...`);
  const range = Math.ceil((max - min) / threadCount);
  let start = min;
  for (let i = 0; i < threadCount - 1; i++) {
    const myStart = start;
    threads.add(new Worker(__filename, { workerData: { start: myStart, range }}));
    start += range;
  }
  threads.add(new Worker(__filename, { workerData: { start, range: range + ((max - min + 1) % threadCount)}}));
  for (let worker of Array.from(threads)) {
    worker.on('error', (err) => { throw err; });
    worker.on('exit', () => {
      threads.delete(worker);
      console.log(`Thread exiting, ${threads.size} running...`);
      if (threads.size === 0) {
        console.log(primes.join('\n'));
      }
    })
    worker.on('message', (msg) => {
      primes = primes.concat(msg);
    });
  }
} else {
  generatePrimes(workerData.start, workerData.range);
  parentPort.postMessage(primes);
}

worker.workerData 是一个任意的 JavaScript 值,它包含传递给该线程的 Worker 构造函数的数据的克隆,因此需要导入它。更多信息 HERE