Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 946
Vinay Madan Avatar asked Jul 26 '17 01:07

Vinay Madan


People also ask

Is VSCode written in TypeScript?

About the App Visual Studio Code is a cross platform code editor written in TypeScript based on Code OSS with support for extensions and a wide range of programming languages.


2 Answers

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);
like image 73
Claytronicon Avatar answered Oct 08 '22 17:10

Claytronicon


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
like image 25
Howard Avatar answered Oct 08 '22 18:10

Howard