How to use generics in TypeScript

Поделиться
HTML-код
  • Опубликовано: 18 сен 2022
  • Official Generics Docs: www.typescriptlang.org/docs/h...
    shaky.sh
  • НаукаНаука

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

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

    👋there are more great ways to use generics, so I wrote about them here: shaky.sh/ts-generic-utils/

  • @DemPilafian
    @DemPilafian Год назад +6

    One of the key things that made this tutorial awesome is the excellent *"job queue"* example. Using a simple but realistic example is a million times more helpful than using the typical meaningless placeholders of *"foo", "bar", "test1", "test2",* and *"asdf".*

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

    Your channel is such a gem

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

    Why have I watched like 10 videos on the subject of Generics but only now understanding it. You did a very good job explaining this, THANKS !

  • @rolikaseventysix
    @rolikaseventysix Год назад +16

    I've discovered your channel a few days ago and started to watch all your videos. You're are amazing buddy! State of the art content and the way you explain everything to the detail is unbeatable! The length of your videos also fits me a lot. Thank you and keep doing the good stuff!

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

    Had read the official docs numerous times and had watched several vids regarding generics in TS. But couldn't develop a firm mental model about the same like your vid helped me develop. Amazing explanation man ! 👌

  • @BrandonKBoswell
    @BrandonKBoswell 7 месяцев назад

    RUclips is acting bizarrely. It says this video has 30K views, but has 45K likes. That’s a great ratio. Keep up the great work! Also the video was great. Thank you!

  • @JosimarBezerra-fx8wb
    @JosimarBezerra-fx8wb Год назад +3

    For ages I've been coming across tutorials and articles on this topic but this video is exactly what I've been looking for. I can finally say that now I totally got my head around how generics work in TypeScript. Thank you Andrew for making this incredibly helpful video. You've definitely gotten my subscription. Keep up with the great content.

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

    best explanation for generics ever

  • @TimothySmithDev
    @TimothySmithDev Год назад +3

    Excellent video, as always

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

    This was a well explained video with good pacing and editing. Subscribed!

  • @TheDanthelion
    @TheDanthelion 6 месяцев назад

    Great practical example, really helped clear things up

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

    One of the best explanations of Generics I've ever seen 👏

  • @Cypekeh
    @Cypekeh 9 месяцев назад +1

    I like the word "job" now

  • @mmmike3426
    @mmmike3426 Год назад +3

    Hype, I'm sure this is gonna be a masterclass!!

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

      Nice example!! I usually see generics being used in mostly contrived ways, but the extends here was a really neat way to show the potential - I think this even helped give the factory pattern a bit more context for me!

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

    thanks a lot for this , it helps me understand generics in a deep way

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

    Super helpful! Thanks a lot for this!

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

    Fantastic explanation! Bravo man

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

    The thumbnail is pretty clever

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

    I often find generic pretty hard to implement in my code base, but I just got an idea how I can do it now. Thanks!

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

    Subscribed! Thank you.

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

    Andrew, good seeing you again. Took few of your class at Net Tuts Plus, Great teacher

  • @RayanMADAO
    @RayanMADAO 11 месяцев назад

    Amazing thanks for helping me understand generics. I have a more intuitive feel for them now.

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

    This was helpful to form a Nice mental modal

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

    Your the the best!

  • @kirillvoloshin2065
    @kirillvoloshin2065 11 месяцев назад

    template type analogy is great!
    I would love if TS renamed generics to template types of something

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

    great content. thanks

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

    Very nice explanation, thank you very much! ;)

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

    Thank you very much

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

    Great video!

  • @domoknows3275
    @domoknows3275 6 месяцев назад

    great example!

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

    Thumbnail got me

  • @Viva-07
    @Viva-07 7 месяцев назад

    Thank you..nice video

  • @KarimFayed-oy5oo
    @KarimFayed-oy5oo Год назад

    Great video!
    The one thing I don't understand is why is there a need for the angle brackets before the parenthesis of a function?

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

    Dude you’re good at explaining things. Keep it up! 💯 Glad I discovered your channel. Btw, will you make a video on types vs interfaces? Or do you have a short explanation on when to use one over the other? Thank you!

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

      I’m working on that video right now :) should be out some time in the next few weeks.

  • @anatholekaringanire7649
    @anatholekaringanire7649 7 месяцев назад

    Nice tip

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

    Great, clear explanation. I fully understand why it's called a "generic" in this example, since it can contain basically any value. But I still don't fully grasp why its called a generic when we annotate the useState in React like so for example: useState(false); This seems very specific (either an object with a specific set of props or a boolean as fallback), still this is called a "generic". Not understanding why...

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

    Why create `Job` as a type instead of an interface?

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

    If you have any questions... 😂😂😂. I do have!!! Thanks. 👍

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

    I'd rather to use more readable names on the generics.. it does not hurt having a code that is easy to read.

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

    GEN.... I have a suggestion for a video... what is TNext in this TypeScript «interface Generator»

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

      I thought my question was so unrelated until I realized that it is indeed...

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

    Hey, I have a question, but it's actually not related to the topic main of this video exactly.
    I noticed you are using this // ^? comment at the bottom, which seems rather useful. Can you say exactly what it's called exactly? I tried searching for it online but couldn't find anything. Is it a typescript specific thing, or perhaps it's just a typescript playground feature? If it's documented somewhere, a link to that would be golden, cheers!

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

      Yeah! I think I first saw it in a Matt Pocock video, but it’s called a twoslash query:
      www.typescriptlang.org/play?#handbook-15
      (Edit: RUclips doesn’t seem to like the hash sign in that link, so maybe copy and paste it instead of clicking?)

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

      @@andrew-burgess Thanks for the link, copy-pasting worked as intended so highly appreciated :)
      The reason I asked is because I thought it can be useful having something like that in VSCode, and sure enough it appears there is an extension with the same name available so I am probably going to give it a try.
      So thanks again, and keep up the great work. Your channel and content are amazing, definitely one of the best ones about Typescript along with Matt.
      EDIT: Also, just a small tip/idea/feedback, you could leave a TS playground links with the finished and/or starting version of the code you show on the screen in the description of your videos. Perhaps that could be useful for people who want to follow along but don't want to re-create the whole example from scratch. But up to you of course, cheers.

  • @andyl.5998
    @andyl.5998 Год назад +1

    10:13 The naming of "intersection" in TS confuses me a bit. Normally, one would expect the operation of "intersection" to yield something smaller, by extracting the common part of multiple sets.
    "Intersection" in TS seems to function as "union" of properties. Although union is already taken in TS to mean the union of types, the naming of "intersection" still confuses me. 😅

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

      Exactly! I would just change the mental model to mean AND/OR in the boolean sense.

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

      lol, you're so spot-on. I've had the same conversation in my head multiple times.
      But the intersection is smaller, and only what is common; the union is broader, just like you'd expect.
      type toys = "ball" | "bat" | "shovel" | "bucket";
      type events = "ball" | "gala" | "soiree" | "benefit"
      type OnlyBall = toys & events;
      type AllItems = toys | events;
      Dunno if this makes it more or less confusing ... let me know if this would be a good video topic!

    • @andyl.5998
      @andyl.5998 Год назад

      @@andrew-burgess Yeah, OnlyBall and AllItems make total sense. The incongruence exists only in cases such as Job & {...} at 10:13.

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

      One mental model that's helped me with this is that even though the type itself _seems_ broader (since there are more fields), the set of values that match that type is a much smaller set, because it now has to match more fields.
      For example:
      type HasName = { name: string };
      type HasAge = { age: number };
      type HasNameAndAge = HasName & HasAge;
      type HasNameOrAge = HasName | HasAge;
      const a = { name: "Andrew" };
      const b = { age: 100 };
      const c = { name: "Andy", age: 200 };
      // HasName: a, c
      // HasAge: b, c
      // HasNameAndAge: c
      // HasNameOrAge: a, b, c
      The types `HasName` and `HasAge` will be matched by two objects. HasNameOrAge is a union, and therefore matches a broader set of shapes: all 3 shapes. HasNameAndAge is an intersection, and so matches a narrower set of only 1 shape.
      I learned a lot of this from this blog post: ivov.dev/notes/typescript-and-set-theory

    • @andyl.5998
      @andyl.5998 Год назад

      @@andrew-burgess Wow! Just wow! Your explanation is pure gold, Andrew. Thank you so much. My mind is blown.

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

    immediately subscribed plus he's cute

  • @AlexJohnSuarez
    @AlexJohnSuarez Год назад +3

    You look like spiderman

  • @jessejoseph2643
    @jessejoseph2643 3 месяца назад

    This guy has just complicated the whole concept of generics. Anyone who really wants to wrap their head around generics should go over to web dev simplified ruclips.net/video/EcCTIExsqmI/видео.html. You are welcome.

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

    too much information... gotta re-watch it tho

  • @hari9321
    @hari9321 10 месяцев назад

    This is one of the simplest and easy to understand tutorial about generics. Thanks @andrew-burgess