Nirvana
Nirvana

Reputation: 643

What is the difference between "number" and "Number" in TypeScript?

In TypeScript, there are two distinct "number" types. The first one is called lowercase number, and the second is uppercase Number. If you try printing number, there is compiler error:

console.log(number); //-> error TS2693: 'number' only refers to a type

On the other hand, printing Number will give the standard function description:

console.log(Number); //-> [Function: Number]

This is unsurprising as Number is just a built-in JS constructor documented here. However, it is unclear, what number is supposed to be.

Judging by the error message, it seems that number isn't actually a discrete value(?!) like Number. But despite this, it is used in variable and function declarations as if it is a value, like:

var two: number = 2;
function sqr(x: number) { return x; }

On the other hand, user-defined types like classes appear to be discrete values (as they also print the standard function description). And, to further complicate matters, Number can be used in annotations similar to number:

var two: Number = 2;

There is similar cases with string and String, any and Object, etc.

So, my question is: What are number, string, etc in TypeScript, and how are they different then built-in constructors?

Upvotes: 32

Views: 23966

Answers (2)

Bergi
Bergi

Reputation: 665000

From https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean:

JavaScript has three very commonly used primitives: string, number, and boolean. Each has a corresponding type in TypeScript. As you might expect, these are the same names you’d see if you used the JavaScript typeof operator on a value of those types:

  • string represents string values like "Hello, world"
  • number is for numbers like 42. JavaScript does not have a special runtime value for integers, so there’s no equivalent to int or float - everything is simply number
  • boolean is for the two values true and false

The type names String, Number, and Boolean (starting with capital letters) are legal, but refer to some special built-in types that will very rarely appear in your code. Always use string, number, or boolean for types.

(There are also typs for null and undefined)

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 371019

number is only a TypeScript thing - it's a primitive type referring to, well, a number.

But, judging by the error message, it seems that number isn't actually a discrete value like Number.

Indeed - it's a type, so it doesn't exist in emitted code.

a discrete value like Number. On the other hand, user-defined types like classes appear to be discrete values (as they also print the standard function description).

Yes. Classes, of which Number is one, are special. They do two things, somewhat unintuitively:

  • They create a JavaScript class (usable in emitted code)
  • They also create an interface for the class (only used by TypeScript)

If you use Number in a place where a type is expected, TypeScript will not complain, because Number is an interface.

If you use Number in a place where a value (something that exists in emitted code) is expected, TypeScript will not complain, because Number is also a global constructor.

In other words, the two Numbers below refer to completely different things:

// refer to the TypeScript Number interface
let foo: Number;

// refer to the JavaScript global.Number constructor
const someNum = Number(someString);

Using Number in TypeScript is very odd, since it'd, strictly, speaking, refer to a number created via new:

const theNum = new Number(6);

Which there's almost never a reason to do. Use a plain primitive number instead, without an object wrapper.

const theNum = 6;
// theNum is typed as `number`

Upvotes: 31

Related Questions