Within the realm of TypeScript, there exist two distinct variations of the "number" type. The first is denoted as lowercase number
, whereas the second is represented in uppercase as Number
. Attempting to display number
results in a compiler error:
console.log(number); //-> error TS2693: 'number' only refers to a type
In contrast, viewing Number
provides the standard function description:
console.log(Number); //-> [Function: Number]
This behavior is anticipated since Number
serves as a pre-existing JavaScript constructor documented here. Nevertheless, the nature of number
remains ambiguous.
From the error message, it appears that number
does not embody a distinct value like
Number</code. Despite this, it is utilized in variable and function declarations as if it were a value:</p>
<pre class="lang-js"><code>var two: number = 2;
function sqr(x: number) { return x; }
In contrast, user-defined types such as classes appear as separate values (and exhibit the standard function description when printed). Adding complexity, Number
can be employed in annotations similarly to number
:
var two: Number = 2;
Similar scenarios arise with string
and String
, any
and Object
, and so forth.
Therefore, the inquiry stands: What exactly are number
, string
, etc in TypeScript, and how do they differ from the built-in constructors?