Search code examples
javascriptangulartypescriptsyntax-errorunexpected-token

Angular declare var x:any; unexpected token 'var'


I have a problem, I am building an angular application and created a TS File that works with my API (imported in the index.html with a script tag). I am exporting functions out of this file to use them in my components. Everything is working but I still get an error in my Browser. (IntelliJ is also not showing me an error) My code is:

declare var AirConsole:any;

let airconsole: any;

export const enum MessageClass {
  ERROR,
  LOGIN,
ANNOUNCEMENT
}

export function InitAirConsole(){
  airconsole = new AirConsole();
  console.log("Created AirConsole Controller:" );
  console.log(airconsole);

  airconsole.onMessage = function(from:number,data:any){
    switch(data.messageClass){
      case MessageClass.ERROR:
        console.log("Error Code: " + data.errorCode + "\nDetails: " + data.errorDetails);
        break;
      case MessageClass.ANNOUNCEMENT:
        console.log("Announcement from Screen\nHeader: " + data.header + "\nMessage:" + data.message);
        break;
    }
  }
}

export function SendMessageToScreen(data:any){
  airconsole.message(AirConsole.SCREEN, data);
  console.log("Data sent to Screen. Data: ");
  console.log(data);
}

The error has to be in line 1. It says Uncaught SyntaxError: Unexpected token 'var' Error


Solution

  • The problem is that my ts file is listed in my angular.json within the scripts array. I think thats why the typsecript code was used in my script.js. Removing it from the script array fixed the issue.