Logo

Home

About

Blog

Contact

Buy Merch

Portfolio

Privacy

TOS

Click to navigate

  1. Home
  2. Joshua R. Lehman's Blog
  3. Object Property Modifiers

Table of contents

  • Share on X
  • Discuss on X

Related Articles

Introduction to Interfaces
TypeScript
8m
Jan 12, 2026

Introduction to Interfaces

Interfaces in TypeScript define contracts for objects, specifying the shape and behavior expected. This guide covers how to create interfaces, use optional properties, and apply readonly modifiers for safer code.

#TypeScript#Interfaces+3
Object Types and Type Annotations
TypeScript
6m
Jan 7, 2026

Object Types and Type Annotations

Objects are fundamental in TypeScript. This guide covers how to define object types using inline annotations, handle nested objects, and work with object literals to ensure type safety and clarity.

#TypeScript#Objects+3
Mastering TypeScript Utility Types
TypeScript
9m
Feb 1, 2026

Mastering TypeScript Utility Types

TypeScript provides powerful built-in utility types that help you transform existing types without rewriting them. This guide covers the most essential utility types and how to use them effectively in your projects.

#TypeScript#Utility Types+4
Ask me anything! 💬

© Joshua R. Lehman

Full Stack Developer

Crafted with passion • Built with modern web technologies

2026 • All rights reserved

Contents

  • Optional Properties (?)
  • Readonly Properties
  • Required Properties
  • Utility Types for Modifiers
  • Best Practices
  • Common Mistakes
  • Next Steps
TypeScript

Object Property Modifiers

January 22, 2026•2 min read
Joshua R. Lehman
Joshua R. Lehman
Author
Object Property Modifiers in TypeScript
Object Property Modifiers

Object Property Modifiers#

TypeScript provides several property modifiers to control how object properties behave. These modifiers help you create more precise and safer type definitions.

Optional Properties

Mark properties as optional using the ? modifier. This allows objects to omit those properties:

interface User {
  name: string;
  email?: string; // optional
}
 
const user1: User = { name: "Alice" }; // Valid
const user2: User = { name: "Bob", email: "[email protected]" }; // Also valid

Optional properties are useful when some data might not be available.

Readonly Properties

Use the readonly modifier to prevent properties from being modified after initialization:

interface Config {
  readonly apiKey: string;
  readonly timeout: number;
}
 
const config: Config = { apiKey: "secret123", timeout: 5000 };
// config.apiKey = "newkey"; // Error: Cannot assign to 'apiKey' because it is a read-only property

Readonly properties help enforce immutability and prevent accidental changes.

Required Properties

By default, properties in interfaces are required. You can also explicitly make optional properties required using utility types:

interface PartialUser {
  name?: string;
  email?: string;
}
 
// Make all properties required
type CompleteUser = Required<PartialUser>;
 
const user: CompleteUser = { name: "Alice", email: "[email protected]" };

Utility Types for Modifiers

TypeScript provides utility types to manipulate property modifiers:

  • Partial<T>: Makes all properties optional
  • Required<T>: Makes all properties required
  • Readonly<T>: Makes all properties readonly
  • Pick<T, K>: Selects specific properties
  • Omit<T, K>: Excludes specific properties

Example:

interface Product {
  id: number;
  name: string;
  price: number;
  description?: string;
}
 
// All properties optional
type PartialProduct = Partial<Product>;
 
// All properties readonly
type ReadonlyProduct = Readonly<Product>;

Best Practices

  • Use optional properties for data that may not always be present
  • Apply readonly to properties that should not change after initialization
  • Leverage utility types to transform property modifiers as needed
  • Be consistent in your use of modifiers across your codebase

Tip: Combine modifiers like readonly and ? for fine-grained control: readonly name?: string

Common Mistakes

  • Forgetting that optional properties can still be undefined
  • Attempting to modify readonly properties at runtime
  • Overusing optional properties, leading to less predictable code
  • Not leveraging utility types for common modifier transformations

Next Steps

Next, dive into index signatures to handle objects with dynamic keys and create flexible dictionary-like structures.