Basic Types & Annotations

Difficulty: Beginner

Question

What are the basic types in TypeScript? Explain the difference between explicit type annotations and type inference, and when you should write annotations yourself.

Answer

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.

The primitive and special types

TypeExampleNote
string"Swiggy"Use lowercase; String is the wrapper object type
number499, 0.1One type for ints and floats (IEEE 754 double)
bigint10nArbitrary precision integers, needs target ES2020+
booleantrue
symbolSymbol("id")unique symbol for const declarations
null, undefinedSeparate types under strictNullChecks
voidfunction return"Return value is not meaningful"
neverfunction that always throwsNo value can have this type
unknown / anySafe top type vs opt-out of checking

How inference works

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.

When to write annotations

void vs undefined

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).

Common mistakes

How TypeScript decides a variable's type. Annotations win; otherwise the compiler infers from the value and context.
  1. Explicit annotation present?: If yes, that type is used and the value is checked against it.
  2. Infer from the initialiser: let x = 0 becomes number.
  3. Apply widening rules: const keeps literal 0, let widens to number.
  4. Use contextual typing: Callback params get types from where they are passed.
  5. Fall back to implicit any: Flagged as an error under noImplicitAny.

Code examples

Annotations vs inference

// 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.

void, undefined and never

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.

Key points

Common follow-up questions

Interview tips

Concepts covered

Primitive Types, Type Annotations, Type Inference, Literal Widening, void and undefined

More TypeScript Interview questions