Union Types in TypeScript
HTML-код
- Опубликовано: 28 июл 2024
- A union type in TypeScript allows you to declare a value that can have different types.
My FREE programming apps:
- apps.apple.com/app/id659282498
- play.google.com/store/apps/de...
My FREE TypeScript Course:
- typescript.tv/
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:
➜ Unions & Intersections: www.typescriptlang.org/docs/h...
➜ Venn diagram: en.wikipedia.org/wiki/Venn_di...
➜ Type aliases: www.typescriptlang.org/docs/h...
➜ Discriminated Unions: basarat.gitbook.io/typescript...
Follow TypeScript TV:
🌐 typescript.tv
🐦 / typescripttv
📺 / typescripttv
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
}
Thank you, it's very helpful!
Such a great content! Thank you!
My pleasure! Thank you for your comment. 😊