Difficulty: Beginner
What are the basic types in TypeScript? Explain the difference between explicit type annotations and type inference, and when you should write annotations yourself.
TypeScript's basic types mirror JavaScript's values: string, number, boolean, bigint, symbol, null, undefined, plus object, arrays, and special types like any, unknown, void and never. You can annotate a variable explicitly (let count: number = 0), but most of the time TypeScript infers the type from the value, so let count = 0 is already number. Good TypeScript code annotates function boundaries and lets inference handle the rest.
| Type | Example | Note |
|---|---|---|
string | "Swiggy" | Use lowercase; String is the wrapper object type |
number | 499, 0.1 | One type for ints and floats (IEEE 754 double) |
bigint | 10n | Arbitrary precision integers, needs target ES2020+ |
boolean | true | |
symbol | Symbol("id") | unique symbol for const declarations |
null, undefined | Separate types under strictNullChecks | |
void | function return | "Return value is not meaningful" |
never | function that always throws | No value can have this type |
unknown / any | Safe top type vs opt-out of checking |
TypeScript infers from initialisers, return statements, default parameters and contextual position. const total = 499 gets the literal type 499 because a const cannot change, while let total = 499 is widened to number. Callback parameters are inferred contextually: in orders.map(o => o.id), o is already typed from the array. Function return types are inferred from the return statements.
any, which noImplicitAny flags).isolatedDeclarations (TS 5.5+).let items: Order[] = [] or useState<User | null>(null), because there is nothing to infer from.const name: string = "Asha" is noise. Let inference do it.void as a return type means callers should ignore the value. A callback typed () => void may actually return something (that is why arr.forEach(x => list.push(x)) is allowed even though push returns a number). undefined is a real value type: a function returning undefined must actually return it (TS 5.1+ allows omitting the return statement).
Number, String, Boolean (wrapper objects) instead of lowercase primitives.number can safely hold money as rupees with decimals; store paise as integers or use bigint for very large values.// Inferred, no annotation needed
let city = "Bengaluru"; // string
const gst = 18; // literal type 18
let isOpen = true; // boolean
const big = 9007199254740993n; // bigint
// Annotate when there is nothing to infer from
let cart: string[] = [];
let selected: number | null = null;
// Annotate parameters; return type is inferred as number
function addGst(pricePaise: number, rate = gst) {
return Math.round(pricePaise * (1 + rate / 100));
}
// Contextual typing: o is inferred from the array
const orders = [{ id: "a1", amountPaise: 1200 }];
const ids = orders.map((o) => o.id); // string[]
Only the empty array, the nullable variable and the function parameter need annotations. Everything else is inferred precisely.
function logEvent(name: string): void {
console.log(name);
}
function fail(message: string): never {
throw new Error(message);
}
// A () => void callback may return a value; it is ignored
const list: number[] = [];
[1, 2, 3].forEach((n) => list.push(n)); // push returns number, still OK
function parseRate(input: string): number {
const n = Number(input);
if (Number.isNaN(n)) fail(`Invalid rate: ${input}`);
return n; // n is number; fail() never returns
}
void says the result is irrelevant, never says the function cannot return normally, and control flow analysis uses never to know code after fail() is unreachable.
Primitive Types, Type Annotations, Type Inference, Literal Widening, void and undefined