TypeScript:意外的令牌; '构造函数,函数,访问器或变量'

时间:2016-05-16 11:26:34

标签: javascript types typescript ecmascript-6 tslint

我在类型脚本中编写了下面的类。当我编译它时,它错误地说

"src\main\MqttClientWrapper.ts(24,2): error TS1068: Unexpected token. A construct or, method, accessor, or property was expected."

以下是我的代码。

var mqtt :any = require('mqtt');

export interface IWillMessage {
  topic: string;
  payload: string;
  qos: number;
  retain: string;
}

export interface IMessageReceivedCallBack {
  onMessageReceived(message : string);
}

export interface IMqttOptions {
  clientId: string;
  keepAlive: number;
  clean: string;
  reconnectPeriod: string;
  will: IWillMessage;
}

export default class MqttClientWrapper {

 client : any;

constructor(url: string, mqttOptions : IMqttOptions, messageReceivedCallBack : IMessageReceivedCallBack) {
   client = mqtt.connect(url, mqttOptions);
   client.on('message',function(topic : string, message : string){
     messageReceivedCallBack.onMessageReceived(message);
   }
}

subscribeMessage(topic : string) {
  client.subscribe(topic);
}

publishMessage(topic : string, message : string, level : number ) {
  client.publish(topic,message,level);
}

}

错误指向该行

 client : any;

我尝试过“var client:any;”和“let client:any”。我仍然得到同样的错误。此外,在跟踪中找到了错误行。

src\main\MqttClientWrapper.ts(26,16): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,16):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,38): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,38):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,78): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,78):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,106): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,106):
 error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(31,1): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(31,1): e
rror TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,24): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,24):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,34): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,34):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(37,22): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,22):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,40): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,40):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,56): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,56):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,67): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,67):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(41,1): error TS1128: Declaration or statement expe
cted.

以下是包含行号的代码。

enter image description here

我在这里做错了什么?那些错误也是因为第一个“意外的令牌错误”或那些行中的错误?请指教。

1 个答案:

答案 0 :(得分:10)

您无法使用letvar定义班级成员,只能使用publicprivate或任何内容(或static)。< / p>

所以这就是导致问题的原因:

export default class MqttClientWrapper {
    let client : any; // <- right here

    // ...
}

这只是您附加的图像中的情况,而不是您发布的代码中的情况。