TypeScript : Unexpected token; 'constructor, function, accessor or variable'
Asked Answered
H

1

6

I have the below class written in type script. When I compile it, it errors out saying

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

Below is the code I have.

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);
}

}

The error is pointing to the line,

 client : any;

I have tried " var client :any; " and " let client : any " as well. Still I get the same error. Also below lines of errors are found in the trace..

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.

Below is the code with the line numbers.

enter image description here

What am I doing wrong here?are those errors also there because of the first "unexpected token error" or something wrong in those lines as well? Please advice.

Homeland answered 16/5, 2016 at 11:26 Comment(3)
your code compiles in the playground: typescriptlang.org/play/index.html. What typescript version are you using?Ferule
@RegisPortalez its 1.8.7Homeland
in the code you posted, you don't use let. I guess the error was about the missing parenthesisFerule
D
12

You can't define class members with let or var, you can only use public or private or nothing (or static).

So this is what causes your problem:

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

    // ...
}

This is only the case in the images you attached, not in the code you posted.

Deodorize answered 16/5, 2016 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.