Q4 of 38 · TypeScript
What are the primitive types in TypeScript, and what is the difference between `number`, `Number`, and `Number` the wrapper object?
Short answer
Short answer: TypeScript's primitives are `string`, `number`, `boolean`, `bigint`, `symbol`, `null`, and `undefined`. Lowercase `number` is the primitive type annotation; uppercase `Number` is the JavaScript wrapper object type — always use lowercase in TypeScript. `object` matches any non-primitive; `{}` matches any non-nullish value.
Detail
TypeScript mirrors JavaScript's primitive types with lowercase type annotations.
The seven primitives: string, number, boolean, bigint, symbol, null, undefined. These correspond to the seven primitive value types in JavaScript.
Lowercase vs uppercase: TypeScript has both. number, string, boolean are the correct primitive type annotations. Number, String, Boolean (uppercase) refer to the JavaScript wrapper object types (new Number(5)) — these behave differently from primitives and should never be used in type annotations. The TypeScript compiler will warn if you annotate a variable as Number when you mean the primitive number.
Special types:
any: disables type checking entirely — the escape hatchunknown: the type-safe counterpart toany— must narrow before usenever: a value that can never exist — function that always throws, exhaustive switch defaultvoid: return type of functions that return nothing usefulobject: matches any non-primitive value{}: matches any non-nullish value (includes primitives)
In test automation: Typing API response fields with accurate primitive types prevents assertion errors. number | string union types model polymorphic API responses.
// EXAMPLE
// Correct primitive annotations
let count: number = 42;
let name: string = "Alice";
let active: boolean = true;
let id: bigint = 9007199254740993n;
// WRONG — never use wrapper types
// let count: Number = 42; // Number (wrapper) ≠ number (primitive)
// Special types
function assertNever(x: never): never {
throw new Error("Unexpected value: " + x);
}
type Direction = "up" | "down";
function move(d: Direction) {
if (d === "up") return;
if (d === "down") return;
assertNever(d); // TypeScript knows 'd' is 'never' here
}
// API response types
interface UserResponse {
id: number;
name: string;
email: string;
score: number | null; // null for new users