我尝试在 Typescript 中使用 WebWorkers,但遇到了问题。 我已经安装了 @types/node 并更新了所有内容。 “worker_threads”不再是实验性的,而是在 Node.js 的稳定版本中。
这是我的 typescript 配置:
{
"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"
]
}
当我尝试像这样导入我的 typescript 文件时:
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。 谢谢。
最佳答案
您可以使用 npm 包“worker-loader”。转到下面的链接https://www.npmjs.com/package/worker-loader并引用“与 TypeScript 集成”部分
关于node.js - 使用worker_threads的 typescript 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258924/