Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Using Type Aliases and Interfaces | Section
TypeScript for Backend Development

bookUsing Type Aliases and Interfaces

Свайпніть щоб показати меню

When working with objects, you often reuse the same structure in multiple places. Instead of rewriting the type every time, you can create a reusable definition.

Type Aliases

A type alias lets you define a custom type:

type User = {
  name: string;
  age: number;
};

You can now use this type anywhere:

let user: User = {
  name: "Alice",
  age: 30,
};

Interfaces

Interfaces provide another way to define object structure:

interface User {
  name: string;
  age: number;
}

Used the same way:

let user: User = {
  name: "Bob",
  age: 25,
};

Type vs Interface

At a beginner level, both are very similar.

You can use either one to:

  • Define object shapes;
  • Reuse data structures;
  • Improve readability.

In this track, you can use whichever feels more comfortable.

Arrays with Custom Types

You can also use your types with arrays:

let users: User[] = [
  { name: "Alice", age: 30 },
  { name: "Bob", age: 25 },
];
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 6

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 6
some-alt