2

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

  • 1
    @AlirezaAhmadi The File Where I write the code is a TypeScript File. But I think my mistake was to add the file to my angular.json in the script array. When I remove it there it works :) – Patrick Langkau Jul 14 '21 at 17:25
  • `script.js` doesn't appear to be a TypeScript file. Try renaming it to `script.ts`. – Bergi Jul 14 '21 at 18:03
  • what is `declare`? remove it (and for better style, replace `var` with `let`) – Felix Jul 15 '21 at 01:31

1 Answers1

0

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.