> For the complete documentation index, see [llms.txt](https://koziuk-s.gitbook.io/angular-starter/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://koziuk-s.gitbook.io/angular-starter/base-typescript/robota-z-tipami-danikh.md).

# Робота з типами даних

### Union.

Об'єднання або union не є власне типом даних, але воно дозволяє визначити змінну, яка може зберігати значення двох або більше типів.

Щоб створити union, використовується вертикальну риска `|` .

{% code title="main.ts" %}

```typescript
let id: number | string;

id = '1345dgg5';
console.log(id); // 1345dgg5

id = 234;
console.log(id); // 234
```

{% endcode %}

Перевірка типу даних (typeof).

{% code title="main.ts" %}

```typescript
let num = 1200;

if (typeof num === 'number') {
  let result: number = num / 12;

  console.log(result);
} else {
  console.log('invalid operation');
}
```

{% endcode %}

### **Псевдоніми типів.**

TypeScript дозволяє визначати псевдоніми типів за допомогою ключового слова **type**.

{% code title="main.ts" %}

```typescript
type UserId = number | string;

let userId: UserId;

userId = 'asjk45hhj9';

type User = {
    userId: UserId;
}

let user: User;

user = {
    userId: 1
};
```

{% endcode %}
