Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Union and Intersection Types | Advanced Typing and Asynchronous Patterns
TypeScript for JavaScript Developers

bookUnion and Intersection Types

When you need to describe variables, parameters, or functions that can work with more than one type, TypeScript provides union and intersection types. Union types, created using the | symbol, allow a value to be one of several specified types. Intersection types, using the & symbol, combine multiple types into one, requiring a value to satisfy all included types. These features make type definitions more expressive and flexible, especially in scenarios where your code must handle a variety of input shapes or requirements.

1234567
function formatValue(value: string | number): string { if (typeof value === "string") { return value.toUpperCase(); } // value is a number here return value.toFixed(2); }
copy
1234567
function handleInput(input: string | string[]): string { if (Array.isArray(input)) { return input.join(", "); } // input is a string here return input; }
copy

When you use a union type, you often need to determine the actual type at runtime to handle it safely. This process is called type narrowing. TypeScript uses type guards—such as typeof, instanceof, or checking for properties—to help you write code that safely accesses the correct members of each type in a union.

1. What does a union type allow in TypeScript?

2. What is type narrowing in TypeScript?

question mark

What does a union type allow in TypeScript?

Select the correct answer

question mark

What is type narrowing in TypeScript?

Select the correct answer

Все було зрозуміло?

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

Suggested prompts:

Can you explain the difference between union and intersection types with more examples?

How does type narrowing work in more complex scenarios?

Can you show how intersection types are used in TypeScript?

Awesome!

Completion rate improved to 7.14

bookUnion and Intersection Types

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

When you need to describe variables, parameters, or functions that can work with more than one type, TypeScript provides union and intersection types. Union types, created using the | symbol, allow a value to be one of several specified types. Intersection types, using the & symbol, combine multiple types into one, requiring a value to satisfy all included types. These features make type definitions more expressive and flexible, especially in scenarios where your code must handle a variety of input shapes or requirements.

1234567
function formatValue(value: string | number): string { if (typeof value === "string") { return value.toUpperCase(); } // value is a number here return value.toFixed(2); }
copy
1234567
function handleInput(input: string | string[]): string { if (Array.isArray(input)) { return input.join(", "); } // input is a string here return input; }
copy

When you use a union type, you often need to determine the actual type at runtime to handle it safely. This process is called type narrowing. TypeScript uses type guards—such as typeof, instanceof, or checking for properties—to help you write code that safely accesses the correct members of each type in a union.

1. What does a union type allow in TypeScript?

2. What is type narrowing in TypeScript?

question mark

What does a union type allow in TypeScript?

Select the correct answer

question mark

What is type narrowing in TypeScript?

Select the correct answer

Все було зрозуміло?

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

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

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