Interfaces vs Type Aliases: what's the difference?

Поделиться
HTML-код
  • Опубликовано: 16 окт 2022
  • Just how different are type aliases and interfaces? Does it matter which one you use?
    shaky.sh
  • НаукаНаука

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

  • @user-vp7ht2fg1u
    @user-vp7ht2fg1u Год назад +19

    Man. You have amazing teaching skills. Every difficult issue becomes absolutely clear after watching you video!
    I will recommend your channel everybody!
    Thank you so much

  • @ilonachan
    @ilonachan Год назад +10

    I think with the "amalgamation" property of interfaces at the end, we can sum up this video in a very poetic way: "use types for types, and interfaces for interfaces." if you're defining a shorthand for a commonly used type of thing, that may involve unioning and all kinds of stuff, then use a type alias for that (you might have to anyway). if you're instead defining the structure of an API or class interface, which is supposed to be rigid and have no surprises, then interfaces have the slight advantage. Does this make sense?

  • @vimux
    @vimux Год назад +1

    10 mins ago, I'm scratching my head because Idk what to use and I see a bunch of docs or codebase using type and interface but I don't really know the difference. I got a lot of "Aha" moments watching this. Thank you man! Subscribed!

  • @kbsanders
    @kbsanders Год назад +1

    Clear. Concise. Expertly explained. Well done!

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

    Man I find it madness that interfaces and types are so interchangeable

  • @jcollins519
    @jcollins519 Год назад +1

    Great video! I'd love to see more on the last topic you mentioned about having a global API interface that each module is capable of extending and the related gotchas that come along with that

  • @aram5642
    @aram5642 Год назад +8

    As always, one darn useful video! And I had a happy moment seeing that you also use a type for things like UserId! For exact same reason! I'd be curious to see a video about abstract classes. I think I know when to use them (esp in Angular) but it would be good to hear your explanation anyway.

    • @andrew-burgess
      @andrew-burgess  Год назад

      Great idea, thanks!

    • @aram5642
      @aram5642 Год назад

      @@andrew-burgess It would be super useful to hear your lecture about the 'declare' keyword. It can be confusing. Especially with const or let. After all these are all declarations, so why declare declarations ;) And how is 'declare function' different from specifying a function signature type, as in type PredicateFn = (value: any) => boolean (for example).

    • @DavidAguileraMoncusi
      @DavidAguileraMoncusi Год назад

      I use them too. But it bug me that they're only aliases and TS doesn't complain when i mix them up. Or should I say it USED to bug me? I discovered Andrew's video on Branded Types and I'm now using those instead (via a package named ts-brand). Totally recommend it!

  • @kouroshg4621
    @kouroshg4621 Год назад

    I personally like to use interfaces for my react component props and type for all other objects used. I come from c# and can’t help but to see resemblance of classes in react components although vastly different after introduction of hooks.
    Thanks for the in-depth comparison!

  • @FirstLast-kv1iq
    @FirstLast-kv1iq Год назад +2

    This was super useful! Thank you so much

  • @matthewlefevre5667
    @matthewlefevre5667 Год назад

    Very helpful. Thank you!

  • @producdevity
    @producdevity Год назад +1

    Your video’s are great! I was wondering why they didn’t pop up in my feed earlier, then I saw that nowhere in the title or description you mention typescript. Might be helpful for youtube algorithm to add it

  • @ajgubi19
    @ajgubi19 Год назад

    Many thanks. Compared to other of the "popular" channels, your videos are simpler to understand. Please keep making videos. Subscribed. 💯

  • @avi3681
    @avi3681 Год назад

    Very clear video.

  • @kugi7786
    @kugi7786 Год назад

    That was really helpful, thank you

  • @pgtechmedia5624
    @pgtechmedia5624 23 дня назад

    Really helpful. Thanks

  • @bubu88134
    @bubu88134 8 месяцев назад

    Amazing

  • @raianmr2843
    @raianmr2843 Год назад +4

    Really just boils down to this:
    Want union types? -> use type alises.
    Want extension methods? -> use interfaces.

  • @saketc2010
    @saketc2010 11 месяцев назад +1

    Confused with type:'admin'. Can you pls elaborate

  • @rahimco-su3sc
    @rahimco-su3sc Год назад

    thanks | defenetly helpfull

  • @aamirsuleman9815
    @aamirsuleman9815 9 месяцев назад

    Adding a key “type” to an extended interface threw me off at 5:00 😂

  • @redcrafterlppa303
    @redcrafterlppa303 Год назад

    I don't know about typescript but what you showed really defeats the purpose of the idea of interfaces. Interfaces are in concept types that define the minimal api surface a certain functionality should have. For example:
    interface List {
    add(val: T) : boolean
    get(index: int) : T
    set(index: int, val: T)
    }
    class ArrayList implements List {...}
    class LinkedList implements List {...}

  • @windar2390
    @windar2390 Год назад

    9:00 - For me thats the only reason to use interfaces.

  • @pmioduszewski
    @pmioduszewski Год назад +1

    sub

  • @adriatic123
    @adriatic123 4 месяца назад

    Problems with once potent javascript ecosystem were started once OOP programmers started to move into js world without respecting its specific strengths. Now we are forced not only to use js in an OOP way which is totally wrong, but they dont stop there but want to mould js js into a total OOP language. This shows not only lack of js knowledge but also a disrespect of all js community. I long for times where there were two independedt words, functional programming js world and OOP world

  • @alexnh502
    @alexnh502 Год назад

    Super helpful, thank you.