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.

>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.

Leave a ReplyCancel reply