Visual Studio Code 'const' can only be used in a .ts file

18,645

Solution 1

Afaik, You can't define static const within the class declaration. you can try something like this

const MAX_WIDTH = 8.5;

class Books {
  get MAX_WIDTH() {
    return MAX_WIDTH;
  }
}

let myBooks = new Books()
alert(myBooks.MAX_WIDTH);

Solution 2

Are you sure it is right javascript syntax?

class Books {
    static const MAX_WIDTH = 8.5;
}

So far as i know, it's not possible to define static property even in ES2015.

You may try some way else, for example:

class Books {
    static get MAX_WIDTH() {
        return 8.5;
    }
}

console.log(Books.MAX_WIDTH);//8.5
Share:
18,645

Related videos on Youtube

Vinay Madan
Author by

Vinay Madan

Automation Specialist with 10+ years of proven experience in the domains of eLearning, Telecom, Security and Big Data. Currently working with SecurePay as QA Lead.

Updated on September 16, 2022

Comments

  • Vinay Madan
    Vinay Madan over 1 year

    I am getting this error while trying a write a basic JS in Visual Studio Code.

    Already tried changing the settings.json ( $workspace/.vscode/settings.json ) but it doesn't work.

      {
         "javascript.validate.enable": false
      }
    

    enter image description here