Basic Interview Questions

What is TypeScript and why use it over JavaScript?

TypeScript is a statically typed superset of JavaScript made by Microsoft. It adds type annotations and a compiler (tsc) that checks types and emits plain JavaScript. Benefits: errors are caught at compile time instead of in production, better autocomplete and refactoring, and self-documenting code. Types are erased, so there is no runtime cost and no runtime type checking either.

Is TypeScript compiled or interpreted? Can the browser run it?

Neither browsers nor Node.js run TypeScript types. tsc (or esbuild/SWC/Babel) transpiles it to JavaScript, which is then run. Node.js 22.6+ can strip simple type annotations itself (--experimental-strip-types), and tools like tsx do it on the fly, but none of these check types; only tsc does.

What is the difference between any, unknown and never?

TypeAcceptsCan you use it directly?
anyeverythingyes, all checks are off (unsafe)
unknowneverythingno, you must narrow it first (safe)
nevernothingrepresents impossible values: functions that always throw, exhausted unions

Use unknown for untrusted data like JSON.parse results and catch variables.

What is type inference? When should you write annotations?

TypeScript infers types from values: let n = 5 is number, a function's return type is inferred from its return statements. Annotate function parameters (always required in strict mode), variables declared without a value, empty arrays, and usually the return types of exported functions. Let inference handle local variables.

What does strict mode do?

"strict": true enables a group of checks: strictNullChecks, noImplicitAny, strictFunctionTypes, strictPropertyInitialization, strictBindCallApply, useUnknownInCatchVariables, noImplicitThis and alwaysStrict. The most important is strictNullChecks, which stops null/undefined from being assigned to every type.

strictNullChecks

function len(s: string | null): number {
  return s.length; // strictNullChecks: 's' is possibly 'null'
}

Compiler output (tsc)

Line 2: error TS18047: 's' is possibly 'null'.

What is the difference between void and undefined as a return type?

void means "the return value should not be used". A function type returning void can be implemented by a function that returns something (e.g. arr.forEach(x => list.push(x)) is allowed even though push returns a number). undefined means the function must literally return undefined.