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

20,524

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.

Share:
20,524
AnOldSoul
Author by

AnOldSoul

It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so!

Updated on May 17, 2020

Comments

  • AnOldSoul
    AnOldSoul almost 4 years

    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.

    • Regis Portalez
      Regis Portalez almost 8 years
      your code compiles in the playground: typescriptlang.org/play/index.html. What typescript version are you using?
    • AnOldSoul
      AnOldSoul almost 8 years
      @RegisPortalez its 1.8.7
    • Regis Portalez
      Regis Portalez almost 8 years
      in the code you posted, you don't use let. I guess the error was about the missing parenthesis