What is the difference between number and Number in TypeScript?
number is the primitive type you should use. Number (capital N) is the type of the wrapper object created by new Number() and is almost never what you want; the same applies to string vs String and boolean vs Boolean.
Explain it simply
JavaScript has two ways to hold a number: the plain value 5, and a box that wraps it, new Number(5). The box is like keeping a 500 rupee note inside a gift envelope: the note is the same, but now it is a different kind of thing.
TypeScript mirrors this. Lowercase number means the primitive. Uppercase Number describes the wrapper object, and because a primitive is assignable to it, the types blur and mistakes slip in. Linters flag the uppercase forms for this reason.
Example
let a: number = 5;
let b: Number = 5; // allowed, but misleading
// a = b; // Error: 'Number' is not assignable to 'number'
b = a; // fine: primitive goes into the wrapper type
const boxed = new Number(5);
console.log(typeof boxed); // "object"
console.log(typeof 5); // "number"
function double(n: number) { return n * 2; } // always write lowercaseWhat to say in the interview
"Lowercase number, string and boolean are the primitive types and the ones to use. Uppercase Number, String and Boolean are wrapper object types. They exist because JavaScript has wrappers, but using them as annotations is a bug magnet, and the typescript-eslint ban-types rule warns against them."
Common mistakes
- Annotating with String or Number because it 'looks more like a class'.