Union Types in TypeScript

preview_player
Показать описание
A union type in TypeScript allows you to declare a value that can have different types.

*My FREE programming apps:*

*My FREE TypeScript Course:*

Timeline:
00:08 - What is a union type?
00:51 - A union in set theory
01:30 - Creating a type alias
02:11 - Benefits of type aliases
02:24 - Common mistakes with unions
03:16 - Union types in index signatures
04:40 - Discriminated Unions (Tagged Unions)
08:08 - Union types and interfaces

Resources:

Follow TypeScript TV:

Hashtags:
#TypeScript #JavaScript #LearnToCode
Рекомендации по теме
Комментарии
Автор

Awesome video, thank you! If you don't want to use a discriminated union (i.e., you want to avoid adding a new property to each object), you can used the "in" operator! TypeScript is deeply integrated with the "in" operator and knows how to tell the difference between the 2 types. For example:

if ('bark' in dogOrPerson) {
// We have access to all the Dog properties
} else {
// We have access to all the Person properties
}

qodesmith