Syncing account progress…

Back

Beginner · 10 min

Object types and interfaces

Describe the shape of an object and reuse that description.

Name an object shape

An interface describes required properties and their types. It helps check the objects your own code creates; it does not validate data arriving at runtime.

interface Product { name: string; price: number }
const product: Product = { name: "Notebook", price: 4 };
console.log(product.name);

Output

Notebook

Use a type alias

A type alias can also name an object shape. Both forms are useful for describing data; choose a clear, consistent convention for the task.

type Point = { x: number; y: number };
const point: Point = { x: 2, y: 3 };
console.log(point.x + point.y);

Output

5

Put it into practice

  1. Read the types and predict the output before running the examples.
  2. Complete the task, then check your understanding with the practice questions.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Supply the required properties

Add "price" with the numeric value 4 to "product". Display its price and keep the "Product" interface.

Practice