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
  • НаукаНаука

Комментарии • 4

  • @qodesmith520
    @qodesmith520 Год назад +2

    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
    }

  • @wilsonwg
    @wilsonwg 2 года назад +1

    Thank you, it's very helpful!

  • @sofiaelkc
    @sofiaelkc 2 года назад +1

    Such a great content! Thank you!

    • @TypeScriptTV
      @TypeScriptTV  2 года назад

      My pleasure! Thank you for your comment. 😊