Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does VS Code colour JavaScript's `Math` and `Number` differently?

Take Math.random and Number.MAX_VALUE function.

Why is Number green and the other Blue (for my colour theme). This also applies to static methods on Number.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you inspect the token scopes using Developer: Inspect Editor Tokens and Scopes, you’ll see:

  • For Math: "semantic token type: variable"

  • For Number: "semantic token type: class"

This makes sense I think. In lib.es5.d.ts:

interface NumberConstructor {
    new(value?: any): Number;
    ...
}
...
declare var Number: NumberConstructor;
...
interface Math {
    ... // no constructor (`new` function)
}
declare var Math: Math;

The new function in the MathConstructor type declaration is providing typings for the Number constructor, which indicates to TypeScript that Number is a class. But not so for the Math type.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading