"Subsequent variable declarations must have the same type"
"Subsequent variable declarations must have the same type"
我正在关注此 link 以使用 webworker-thread 启动示例节点 js 项目。 https://www.npmjs.com/package/webworker-threads
这是我的 ts 代码:
var Worker = require('webworker-threads').Worker;
// var w = new Worker('worker.js'); // Standard API
// You may also pass in a function:
var worker = new Worker(function(){
postMessage("I'm working before postMessage('ali').");
this.onmessage = function(event) {
postMessage('Hi ' + event.data);
self.close();
};
});
worker.onmessage = function(event) {
console.log("Worker said : " + event.data);
};
worker.postMessage('ali');
我收到此错误:
C:\...\node_modules\ts-node\src\index.ts:300
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
^
TSError: ⨯ Unable to compile TypeScript
server.ts (1,5): Subsequent variable declarations must have the same type. Variable 'Worker' must be of type '{ new (stringUrl: string): Worker; prototype: Worker; }', but here has type 'any'. (2403)
server.ts (5,25): Argument of type '() => void' is not assignable to parameter of type 'string'. (2345)
server.ts (6,3): Supplied parameters do not match any signature of call target. (2346)
server.ts (7,3): 'this' implicitly has type 'any' because it does not have a type annotation. (2683)
server.ts (7,29): Parameter 'event' implicitly has an 'any' type. (7006)
server.ts (8,5): Supplied parameters do not match any signature of call target. (2346)
server.ts (12,29): Parameter 'event' implicitly has an 'any' type. (7006)
at getOutput (C:\...\node_modules\ts-node\src\index.ts:300:15)
at C:\...\node_modules\ts-node\src\index.ts:330:16
at Object.compile (C:\...\node_modules\ts-node\src\index.ts:489:17)
at Module.m._compile (C:\...\node_modules\ts-node\src\index.ts:382:43)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\...\node_modules\ts-node\src\index.ts:385:12)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Function.Module.runMain (module.js:604:10)
我不保证是这样,但据我所知,您使用的是节点导入语法。如果你使用 typescript 编译它,我可能会建议你使用 typescript 导入语法:
import {Worker} from 'webworker-threads'
或者,在你的 typescript 编译器配置文件中设置:
"allowJs":true
这可能服务器也启用节点语法。或者你也可以这样做:
var Worker: any = <any>(require('webworker-threads').Worker);
这可能会编译,但您会丢失类型信息。
我正在关注此 link 以使用 webworker-thread 启动示例节点 js 项目。 https://www.npmjs.com/package/webworker-threads
这是我的 ts 代码:
var Worker = require('webworker-threads').Worker;
// var w = new Worker('worker.js'); // Standard API
// You may also pass in a function:
var worker = new Worker(function(){
postMessage("I'm working before postMessage('ali').");
this.onmessage = function(event) {
postMessage('Hi ' + event.data);
self.close();
};
});
worker.onmessage = function(event) {
console.log("Worker said : " + event.data);
};
worker.postMessage('ali');
我收到此错误:
C:\...\node_modules\ts-node\src\index.ts:300
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
^
TSError: ⨯ Unable to compile TypeScript
server.ts (1,5): Subsequent variable declarations must have the same type. Variable 'Worker' must be of type '{ new (stringUrl: string): Worker; prototype: Worker; }', but here has type 'any'. (2403)
server.ts (5,25): Argument of type '() => void' is not assignable to parameter of type 'string'. (2345)
server.ts (6,3): Supplied parameters do not match any signature of call target. (2346)
server.ts (7,3): 'this' implicitly has type 'any' because it does not have a type annotation. (2683)
server.ts (7,29): Parameter 'event' implicitly has an 'any' type. (7006)
server.ts (8,5): Supplied parameters do not match any signature of call target. (2346)
server.ts (12,29): Parameter 'event' implicitly has an 'any' type. (7006)
at getOutput (C:\...\node_modules\ts-node\src\index.ts:300:15)
at C:\...\node_modules\ts-node\src\index.ts:330:16
at Object.compile (C:\...\node_modules\ts-node\src\index.ts:489:17)
at Module.m._compile (C:\...\node_modules\ts-node\src\index.ts:382:43)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (C:\...\node_modules\ts-node\src\index.ts:385:12)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Function.Module.runMain (module.js:604:10)
我不保证是这样,但据我所知,您使用的是节点导入语法。如果你使用 typescript 编译它,我可能会建议你使用 typescript 导入语法:
import {Worker} from 'webworker-threads'
或者,在你的 typescript 编译器配置文件中设置:
"allowJs":true
这可能服务器也启用节点语法。或者你也可以这样做:
var Worker: any = <any>(require('webworker-threads').Worker);
这可能会编译,但您会丢失类型信息。