Skip to content

Chapter 4 of 8

Interfaces, Type Aliases & Object Shapes

Interfaces describe the shape of objects by declaring the names and types of their members. A simple interface Person { name: string; age: number; } tells the compiler that any value typed as Person must have those two properties with those exact types. Because interfaces exist only at compile time, they impose type-checking guarantees without any runtime overhead.

TypeScript also offers type aliases through the type keyword, which can describe unions, intersections, primitives, and other compound types. The choice between interface and type often comes down to intent: interfaces are designed for object shapes and uniquely support declaration merging, while type aliases are more flexible for unions, intersections, and computed utilities. Many developers reach for interfaces when modeling object-oriented contracts and for type aliases when assembling complex type-level logic.

Interfaces and types can be combined or extended in several ways. Index signatures allow dynamic key names for objects whose keys are not known ahead of time, such as interface StringDict { [key: string]: string; }. The readonly modifier marks a property as assignable only at initialization, which is ideal for immutable data. Interfaces inherit members from other interfaces using extends, and unions allow a value to be one of several types with string | number, while intersections require a value to satisfy every type at once with User & { permissions: string[]; }. When the compiler needs a hint about a value's type, type assertions using the as keyword tell TypeScript to treat a value as a specific type, although this should be done cautiously because it bypasses the usual checks.

All chapters
  1. 1Introduction to TypeScript & Setup
  2. 2Basic Types & Type System Fundamentals
  3. 3Functions & Collection Types
  4. 4Interfaces, Type Aliases & Object Shapes
  5. 5Classes & Object-Oriented Programming
  6. 6Generics for Reusable Code
  7. 7Modules & Advanced Type-Level Features
  8. 8Strict Mode & Interoperability with JavaScript

Drill it

Reading is not remembering. These come from the Typescript Essentials deck:

Q

What is TypeScript?

TypeScript is a statically typed superset of JavaScript developed by Microsoft that compiles to plain JavaScript code. It adds optional static typing, interface...

Q

How does TypeScript relate to JavaScript?

TypeScript is a superset of JavaScript, meaning all valid JavaScript code is also valid TypeScript. TypeScript code is transpiled to JavaScript, allowing it to...

Q

How do you install TypeScript?

Install TypeScript globally using npm install -g typescript or locally as a dev dependency with npm install typescript --save-dev. Verify installation with tsc...

Q

What is the TypeScript compiler command?

The TypeScript compiler is invoked with tsc. Use tsc file.ts to compile a single file or tsc to compile based on tsconfig.json.