Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Optional and Readonly Properties | Understanding Interfaces
Working with Interfaces and Generics in TypeScript

bookOptional and Readonly Properties

TypeScript interfaces allow you to control how objects are structured, but sometimes you need more flexibility or safety. Two useful features for this are optional properties and readonly properties. Optional properties are declared using a question mark (?) after the property name, indicating that the property may or may not be present on objects that implement the interface. Readonly properties use the readonly keyword to ensure that, once set, their value cannot be changed.

12345678910111213141516
interface Product { readonly id: number; name: string; description?: string; } const book: Product = { id: 101, name: "TypeScript Handbook" }; const pen: Product = { id: 102, name: "Blue Pen", description: "A smooth-writing blue pen" };
copy

In this example, the Product interface has a readonly id property, a required name property, and an optional description property. When you create a Product object, you must provide an id and name, but description is not required. If you try to assign a new value to id after the object is created, TypeScript will give you an error because id is marked as readonly. On the other hand, you can omit description entirely, or include it if you like. This makes your code saferβ€”preventing accidental changes to properties that should never changeβ€”and more flexible, allowing objects to omit properties that are not always needed.

question mark

Which statement best describes the behavior of optional and readonly properties in TypeScript interfaces?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 9.09

bookOptional and Readonly Properties

Swipe to show menu

TypeScript interfaces allow you to control how objects are structured, but sometimes you need more flexibility or safety. Two useful features for this are optional properties and readonly properties. Optional properties are declared using a question mark (?) after the property name, indicating that the property may or may not be present on objects that implement the interface. Readonly properties use the readonly keyword to ensure that, once set, their value cannot be changed.

12345678910111213141516
interface Product { readonly id: number; name: string; description?: string; } const book: Product = { id: 101, name: "TypeScript Handbook" }; const pen: Product = { id: 102, name: "Blue Pen", description: "A smooth-writing blue pen" };
copy

In this example, the Product interface has a readonly id property, a required name property, and an optional description property. When you create a Product object, you must provide an id and name, but description is not required. If you try to assign a new value to id after the object is created, TypeScript will give you an error because id is marked as readonly. On the other hand, you can omit description entirely, or include it if you like. This makes your code saferβ€”preventing accidental changes to properties that should never changeβ€”and more flexible, allowing objects to omit properties that are not always needed.

question mark

Which statement best describes the behavior of optional and readonly properties in TypeScript interfaces?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt