Logo

Home

About

Blog

Contact

Guestbook

Merch/Store

Portfolio

Privacy

TOS

Click to navigate

  1. Home
  2. Joshua R. Lehman's Blog
  3. Object Types and Type Annotations

Table of contents

  • Share on X
  • Discuss on X

Related Articles

Basic Types in TypeScript
TypeScript
9m
Nov 23, 2025

Basic Types in TypeScript

TypeScript's type system starts with fundamental building blocks: primitive types like string, number, and boolean, along with arrays and type annotations. Understanding these basic types is essential for writing type-safe code. This comprehensive guide walks you through each primitive type, shows you how to use type annotations effectively, and teaches you best practices for working with arrays and basic data structures in TypeScript.

#TypeScript#Types+5
Object Property Modifiers
TypeScript
6m
Jan 22, 2026

Object Property Modifiers

TypeScript provides powerful property modifiers to control how object properties behave. This guide covers optional properties, readonly properties, and required properties to help you create precise and safe type definitions.

#TypeScript#Object Properties+3
Type Aliases vs Interfaces
TypeScript
7m
Jan 17, 2026

Type Aliases vs Interfaces

TypeScript offers both type aliases and interfaces to define types. This guide explains their differences, when to use each, and how to leverage composition patterns for flexible and maintainable code.

#TypeScript#Type Aliases+3

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2026 • All rights reserved

Contents

  • Introduction
  • Defining Object Types Inline
  • Nested Objects
  • Object Literals
  • Best Practices
  • Common Mistakes
  • Next Steps
TypeScript

Object Types and Type Annotations

January 7, 2026•2 min read
Joshua R. Lehman
Joshua R. Lehman
Author
Object Types and Type Annotations in TypeScript
Object Types and Type Annotations

Object Types and Type Annotations#

Objects are a core part of JavaScript and TypeScript. Typing objects correctly helps catch errors early and makes your code more readable and maintainable.

Defining Object Types Inline

You can define the shape of an object inline using type annotations:

function printPerson(person: { name: string; age: number }): void {
  console.log(`${person.name} is ${person.age} years old.`);
}
 
printPerson({ name: "Alice", age: 30 });

This defines an object with name as a string and age as a number.

Nested Objects

Objects can contain nested objects, which you can type inline as well:

type Address = {
  street: string;
  city: string;
  postalCode: string;
};
 
function printAddress(address: {
  street: string;
  city: string;
  postalCode: string;
}): void {
  console.log(`${address.street}, ${address.city} - ${address.postalCode}`);
}
 
const homeAddress: Address = {
  street: "123 Main St",
  city: "Anytown",
  postalCode: "12345",
};
 
printAddress(homeAddress);

You can also nest inline types directly:

function printPersonWithAddress(person: {
  name: string;
  age: number;
  address: { street: string; city: string; postalCode: string };
}): void {
  console.log(
    `${person.name} lives at ${person.address.street}, ${person.address.city}`
  );
}

Object Literals

When you create an object literal, TypeScript infers its type based on the properties:

const car = {
  make: "Toyota",
  model: "Corolla",
  year: 2020,
};

You can explicitly type object literals to ensure they conform to a specific shape:

const car: { make: string; model: string; year: number } = {
  make: "Toyota",
  model: "Corolla",
  year: 2020,
};

Best Practices

  • Use type aliases or interfaces (covered later) for reusable object types.
  • Prefer explicit typing for function parameters and return types.
  • Use nested types or aliases for complex objects to improve readability.
  • Leverage TypeScript’s type inference when possible for object literals.

Tip: Inline object types are great for quick, one-off types but consider interfaces or type aliases for complex or reused shapes.

Common Mistakes

  • Forgetting to type nested objects, leading to implicit any.
  • Mismatching property names or types in object literals.
  • Overusing inline types, which can reduce readability in large codebases.

Next Steps

Next, learn about interfaces in TypeScript to define contracts for objects with optional and readonly properties.


Questions about object types or type annotations? Share your thoughts or examples in the comments!