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
Type Assertions and Type Casting
TypeScript
6m
Dec 13, 2025

Type Assertions and Type Casting

Type assertions tell TypeScript to trust your judgment about a value's type. While they're powerful tools for working with DOM elements, API responses, and type narrowing, they can also bypass type safety if misused. This guide covers the as syntax, angle-bracket syntax, when to use assertions safely, and the crucial difference between TypeScript assertions and runtime type casting. Learn to wield this tool responsibly while maintaining type safety in your applications.

#TypeScript#Type Assertions+4
The any, unknown, and never Types
TypeScript
8m
Dec 8, 2025

The any, unknown, and never Types

TypeScript has three special types that handle edge cases: any (the escape hatch), unknown (the type-safe alternative), and never (for impossible values). Understanding when and how to use these types is crucial for writing safe yet flexible TypeScript code. This guide covers the differences between these types, shows you how to work with unknown safely, and demonstrates how never enables exhaustive type checking and prevents runtime errors. Learn to use these types effectively while maintaining type safety.

#TypeScript#Type System+5

© 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!