Programming languages

TypeScript

A superset of JavaScript with static types that compiles to plain JS.

What it is

TypeScript adds a type system to JavaScript. The compiler checks code before it runs, then strips the types and emits plain JS. TypeScript does not exist at runtime.

Anders Hejlsberg (the author of C#) created it at Microsoft, with a first public release in 2012. Any valid JS is valid TS, so migration can go gradually, file by file.

Types give autocomplete, safe refactoring and catch whole classes of bugs (undefined is not a function) in the editor. Most modern frameworks are written in TS.

Use it when

  • The codebase is long-lived or has a team: types act as living documentation.
  • You need safe refactoring of a large frontend or Node.js backend.
  • You write a library: types become part of the public API.
  • You want shared types between client and server.

Avoid it when

  • A throwaway script or tiny prototype where the build step costs more than it gives.
  • Data arrives from outside without validation: types are not checked at runtime, you still need Zod or similar.

Key concepts

Structural typing
Type compatibility is decided by the shape of an object, not its name. If the fields match, the type fits.
Generics
Types with parameters, like Array<T>: one piece of code works with many types while staying checked.
Union and narrowing
A type A | B plus checks (typeof, in, a discriminant) narrow it to a specific variant.
Utility types
Built-in type transformations: Partial, Pick, Omit, Record, ReturnType.
unknown vs any
any turns checking off, unknown forces you to narrow first. The second is safer.

Example

A discriminated union: the compiler checks that every variant is handled.

Cheat sheet

TypeScript cheat sheet
Command or codeWhat it does
npx tsc --initCreates tsconfig.json
npx tsc --noEmitType check without emitting files
type User = { id: number; name: string }Object type alias
interface Props { title?: string }Interface with an optional field
type Id = string | numberUnion type
function id<T>(x: T): T { return x }Generic function
Partial<User>Makes all fields optional
Pick<User, 'id' | 'name'>Keeps only the listed fields
Record<string, number>Object with string keys and number values
const x = value as constNarrows to literal readonly types
type T = ReturnType<typeof fn>Type of the value a function returns

How to learn it

  1. First know JavaScript well: without it, types only add confusion.
  2. Take the TypeScript course on ShortLesson, then the JS to TS migration course.
  3. Learn basic types, interfaces, unions and literal types.
  4. Study generics and the Partial, Pick, Omit, Record utilities on small examples.
  5. Turn on strict in tsconfig and fix every error in a small project.
  6. Type a real API: describe responses and function parameters.
  7. Read the TypeScript Handbook on the official site.
  8. Practice type narrowing and discriminated unions.

Interview questions

  1. 1. What is the difference between type and interface?

    Both describe an object shape. interface can be extended through declaration merging and extends, while type supports unions, intersections and conditional types. For plain objects either works, consistency matters most.

  2. 2. How does unknown differ from any?

    any turns type checking off for a value. unknown accepts anything but forces you to narrow the type before use. That makes unknown safer for external data.

  3. 3. What are generics and why use them?

    Type parameters that let you write functions and classes for any type while keeping type information. For example, function first<T>(a: T[]): T returns the same type the array holds.

  4. 4. Does TypeScript check types at runtime?

    No, types are erased after compilation. Network data must be validated separately (Zod, Valibot), and the type can be inferred from the schema.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).