Skip to content

TypeScript Interfaces and Type Aliases: Describe Objects, Optional and readonly Properties

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Real data comes in objects: a product has a name, a price and a stock count; an order has an id, a customer and a list of lines. In plain JavaScript the shape of those objects lives only in your head, so a misspelt prise or a forgotten qty turns into undefined somewhere far away. TypeScript lets you write the shape down once, give it a name such as Product, and use that name everywhere. From then on the compiler checks every object you create, every property you read and every function you call with one. This lesson covers the two ways to name a shape, interface and type, and the details you will use daily: optional properties, readonly properties, arrays of objects and combining types.

Quick answer

Describe an object with an interface, interface Product { name: string; price: number }, or with a type alias, type Product = { name: string; price: number }. Mark a property that may be missing with ?, as in discount?: number, and one that must not change with readonly. Use the name as a type anywhere: const items: Product[] = [] or function show(p: Product): string. Interfaces extend each other with extends; type aliases combine with & and can also name unions and other types. Either works for objects; pick one style and stay consistent.

30 min read and practice · Content checked on 17 November 2026

In this lesson you will

  • Name the shape of an object with interface or type and use it as a type
  • Use optional and readonly properties and arrays of objects
  • Extend and combine types with extends and &
  • Explain structural typing and choose between interface and type

Naming the shape of an object

An interface lists the properties an object must have and the type of each: interface Product { name: string; price: number; stock: number }. By convention the name starts with a capital letter. Now const dates: Product = { name: "Ajwa dates", price: 45, stock: 12 } is checked property by property. Leave one out and the compiler says it is "missing in type". Misspell one, such as prise, and it says that "Object literal may only specify known properties", often with a "Did you mean" suggestion. Read a property that does not exist and you get TS2339.

The same shape can be written as a type alias: type Product = { name: string; price: number; stock: number };. Note the equals sign and the semicolon at the end. For a plain object shape, the two are interchangeable and the error messages are the same. Once a shape has a name, use it in function parameters, which is where it helps most: function lineTotal(p: Product, qty: number): number. Inside the function your editor suggests name, price and stock as you type, and outside it every call is checked.

Try it: A Product type catches typos and missing data
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Line 11 misspells price as prise. The compiler reports the unknown property and even suggests the right name; once you fix it, the required price is there too. Line 23 reads a property that does not exist. Fix the typo and delete the last log, and the Console reports no type errors.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

Should I use interface or type in TypeScript?

For object shapes either works, and the TypeScript Handbook leaves it to you. Use type when you need a union, tuple or other non-object type. Many teams default to interface for objects because error messages name it clearly and it can be extended; consistency matters more than the choice.

Do interfaces exist in the JavaScript output?

No. Interfaces and type aliases are types only, so the compiler removes them completely. You cannot check at run time whether an object "is" an interface; you check its properties instead, which the next lesson shows.

How do I type an object whose keys are not known in advance?

Use Record<string, number>, read "an object with string keys and number values", for example stock counts by product code. An index signature, { [code: string]: number }, means the same. Reading a key may give undefined, so check it.

What is the I prefix in names like IProduct?

It is an older naming habit from C# that some codebases still use for interfaces. The TypeScript team does not use it in its own guidelines, and most modern projects write plain names such as Product.

Where should I put my TypeScript types in a project?

Put types used by one file next to the code that uses them, and shared shapes, such as the data an API returns, in a shared file like types.ts that other files import with import type { Product } from "./types".

Sources

Facts in this lesson were checked against these official pages on 17 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.