Interfaces vs Type Aliases: what's the difference?

Поділитися
Вставка
  • Опубліковано 3 січ 2025

КОМЕНТАРІ • 29

  • @ДаниилСоболев-щ4щ
    @ДаниилСоболев-щ4щ 2 роки тому +20

    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

  • @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!

  • @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?

  • @kbsanders
    @kbsanders 2 роки тому +1

    Clear. Concise. Expertly explained. Well done!

  • @aram5642
    @aram5642 2 роки тому +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  2 роки тому

      Great idea, thanks!

    • @aram5642
      @aram5642 2 роки тому

      @@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!

  • @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

  • @ajgubi19
    @ajgubi19 Рік тому

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

  • @saketc2010
    @saketc2010 Рік тому +1

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

  • @producdevity
    @producdevity 2 роки тому +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

  • @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!

  • @biskitpagla
    @biskitpagla Рік тому +4

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

  • @FirstLast-kv1iq
    @FirstLast-kv1iq 2 роки тому +2

    This was super useful! Thank you so much

  • @pgtechmedia5624
    @pgtechmedia5624 6 місяців тому

    Really helpful. Thanks

  • @redcrafterlppa303
    @redcrafterlppa303 2 роки тому

    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 {...}

  • @matthewlefevre5667
    @matthewlefevre5667 Рік тому

    Very helpful. Thank you!

  • @avi3681
    @avi3681 2 роки тому

    Very clear video.

  • @kugi7786
    @kugi7786 Рік тому

    That was really helpful, thank you

  • @aamirsuleman9815
    @aamirsuleman9815 Рік тому

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

  • @bubu88134
    @bubu88134 Рік тому

    Amazing

  • @windar2390
    @windar2390 Рік тому

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

  • @rahimco-su3sc
    @rahimco-su3sc Рік тому

    thanks | defenetly helpfull

  • @adriatic123
    @adriatic123 10 місяців тому

    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

  • @pmioduszewski
    @pmioduszewski 2 роки тому +1

    sub

  • @alexnh502
    @alexnh502 Рік тому

    Super helpful, thank you.