I think there is not a direct way to specify whether a number is integer or floating point. In the TypeScript specification section 3.2.1 we can see:
"...The Number primitive type corresponds to the similarly named JavaScript primitive type and represents double-precision 64-bit format IEEE 754 floating point values..."
I think
intis a bug in Visual Studio intelliSense. The correct isnumber.
I think there is not a direct way to specify whether a number is integer or floating point. In the TypeScript specification section 3.2.1 we can see:
"...The Number primitive type corresponds to the similarly named JavaScript primitive type and represents double-precision 64-bit format IEEE 754 floating point values..."
I think
intis a bug in Visual Studio intelliSense. The correct isnumber.
TypeScript is a superset of JavaScript, which doesn't have a concept of an int. It only has the concept of a number, which has a floating point.
Generally speaking, the amount of work the compiler would have to do to enforce only whole numbers for a TypeScript int type could potentially be massive and in some cases it would still not be possible to ensure at compile time that only whole numbers would be assigned, which is why it isn't possible to reliably add an int to TypeScript.
When you initially get intelliSense in Visual Studio, it isn't possible for the tooling to determine what to supply, so you get everything, including int - but once you are dealing with something of a known type, you'll get sensible intelliSense.
Examples
var myInt: number;
var myString: string;
myInt. // toExponential, toFixed, toPrecision, toString
myString. // charAt, charCodeAt, concat, indexOf, lastIndexOf, length and many more...
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`
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, andboolean. 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:
stringrepresents string values like"Hello, world"numberis for numbers like42. JavaScript does not have a special runtime value for integers, so there’s no equivalent tointorfloat- everything is simplynumberbooleanis for the two valuestrueandfalseThe type names
String,Number, andBoolean(starting with capital letters) are legal, but refer to some special built-in types that will very rarely appear in your code. Always usestring,number, orbooleanfor types.
(There are also typs for null and undefined)