Interfaces vs Type Aliases: what's the difference?

Sdílet
Vložit
  • čas přidán 16. 10. 2022
  • Just how different are type aliases and interfaces? Does it matter which one you use?
    shaky.sh
  • Věda a technologie

Komentáře • 28

  • @user-vp7ht2fg1u
    @user-vp7ht2fg1u Před rokem +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 Před rokem +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?

  • @kbsanders
    @kbsanders Před rokem +2

    Clear. Concise. Expertly explained. Well done!

  • @vimux
    @vimux Před rokem +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!

  • @deltaboukensha
    @deltaboukensha Před rokem +2

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

  • @jcollins519
    @jcollins519 Před rokem +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

  • @FirstLast-kv1iq
    @FirstLast-kv1iq Před rokem +2

    This was super useful! Thank you so much

  • @aram5642
    @aram5642 Před rokem +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  Před rokem

      Great idea, thanks!

    • @aram5642
      @aram5642 Před rokem

      @@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 Před rokem

      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 Před rokem

    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!

  • @ajgubi19
    @ajgubi19 Před 11 měsíci

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

  • @producdevity
    @producdevity Před rokem +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

  • @matthewlefevre5667
    @matthewlefevre5667 Před rokem

    Very helpful. Thank you!

  • @kugi7786
    @kugi7786 Před rokem

    That was really helpful, thank you

  • @avi3681
    @avi3681 Před rokem

    Very clear video.

  • @saketc2010
    @saketc2010 Před 10 měsíci +1

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

  • @bubu88134
    @bubu88134 Před 7 měsíci

    Amazing

  • @rahimco-su3sc
    @rahimco-su3sc Před rokem

    thanks | defenetly helpfull

  • @raianmr2843
    @raianmr2843 Před rokem +4

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

  • @aamirsuleman9815
    @aamirsuleman9815 Před 8 měsíci

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

  • @redcrafterlppa303
    @redcrafterlppa303 Před rokem

    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 Před 10 měsíci

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

  • @pmioduszewski
    @pmioduszewski Před rokem +1

    sub

  • @adriatic123
    @adriatic123 Před 3 měsíci

    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 Před rokem

    Super helpful, thank you.