打字稿配置以使用worker_threads

时间:2019-10-06 15:54:01

标签: node.js multithreading typescript web-worker worker-thread

我正在尝试在Typescript中使用WebWorkers,但遇到了一个问题。 我已经安装了@ types / node并更新了所有内容。 'worker_threads'不再处于实验阶段,它处于node.js的稳定版本中。

这是我的打字稿配置:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "node",
      "dom-mediacapture-record"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

当我试图像这样在我的打字稿文件中导入时:

import { Worker } from 'worker_threads';

出现错误:


This dependency was not found:

* worker_threads in ./src/SpeechRecognizer/SpeechRecognizer.ts

To install it, you can run: npm install --save worker_threads
Type checking and linting in progress...
No type errors found
No lint errors found
Version: typescript 3.6.3, tslint 5.20.0

我还使用Vue.js作为前端。 我已经尝试了所有可能找到的东西,因此我很乐意提供帮助。 如果这不起作用,我将适当地切换到JavaScript。 谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用npm软件包“ worker-loader”。转到下面的链接https://www.npmjs.com/package/worker-loader,然后参考“与TypeScript集成”部分