Sub topic 1
Object Types and Their Properties in TypeScript
Prerequisites
This chapter assumes basic TypeScript syntax, type annotations, interfaces, type aliases, union types, and JavaScript objects and arrays. You should know that a type describes which values a variable may hold.
An object type describes the shape of an object. Its shape is the set of properties the object has, along with each property's type. For example, this interface describes a book:
interface Book {
title: string;
pages: number;
published: boolean;
}
const handbook: Book = {
title: "TypeScript Basics",
pages: 240,
published: true,
};Book is an interface. An interface is a named description of an object shape. The variable handbook must have a title property containing a string, a pages property containing a number, and a published property containing a boolean.
The compiler checks both property names and property types. This assignment fails because pages is a string:
const brokenBook: Book = {
title: "TypeScript Basics",
pages: "240",
published: true,
};The compiler reports that string cannot be assigned to number. Catching this error before the program runs prevents later calculations from treating text as a number.
Interfaces and Type Aliases
You can describe the same object shape with a type alias:
type Book = {
title: string;
pages: number;
published: boolean;
};For the basic object shapes used in this chapter, interfaces and type aliases work similarly. Both can name a type and both can be used in variable annotations, function parameters, and return types.
A type alias can also combine types with unions and other type expressions:
type BookFormat = "paperback" | "ebook";
interface CatalogEntry {
book: Book;
format: BookFormat;
}
function describe(entry: CatalogEntry): string {
return `${entry.book.title} (${entry.format})`;
}Here, format can contain only one of two string literals. A string literal type is a type containing one exact string value.
Reading and Writing Properties
Use dot notation to read a property:
const title = handbook.title;
console.log(title);TypeScript infers that title is a string because handbook.title is declared as a string.
You can assign a new value to a property if the property is not otherwise restricted:
handbook.pages = 250;The new value must still match the declared type. This fails:
handbook.pages = "250";You can also use bracket notation when the property name is stored in a variable:
const propertyName: "title" = "title";
console.log(handbook[propertyName]);A property access is safe only when TypeScript knows that the property exists on the object type. Accessing handbook.author produces an error because author is not part of Book.
Structural Typing
TypeScript primarily uses structural typing. This means that compatibility depends on the required properties, not on the type's name.
interface Book {
title: string;
pages: number;
}
const data = {
title: "Practical TypeScript",
pages: 180,
isbn: "978-0000000000",
};
const book: Book = data;data has all the properties required by Book, so the assignment is allowed. Its additional isbn property does not prevent compatibility.
TypeScript applies a stricter excess property check to an object literal written directly where a type is expected:
const book: Book = {
title: "Practical TypeScript",
pages: 180,
isbn: "978-0000000000",
};This usually fails because isbn is not declared in Book. Assigning the object to an intermediate variable, as in the previous example, avoids that particular check. This behavior helps catch misspelled property names while preserving structural compatibility.
Nested Object Types
Object properties can themselves have object types:
interface Author {
name: string;
country: string;
}
interface Publication {
title: string;
author: Author;
}
const publication: Publication = {
title: "Typed Systems",
author: {
name: "Ada Lovelace",
country: "United Kingdom",
},
};The nested object must satisfy Author. This lets the compiler check a complete object graph, not only the first level.
Summary
An object type defines a value's property names and property types. Interfaces and type aliases give these shapes useful names. TypeScript checks property reads, writes, nested objects, and object compatibility. Its structural type system focuses on the required shape, while excess property checks catch unexpected properties in direct object literals.
Exercise
Why is const entry: Book = { title: "Guide", pages: 100, author: "Sam" } rejected when an object stored in a variable with the same three properties can be assigned to Book?
Show answer
The direct object literal receives an excess property check, which reports author because it is not declared in Book. A previously stored variable is checked structurally and may be assigned when it contains all required Book properties.