How to use generics in TypeScript

Sdílet
Vložit
  • čas přidán 18. 09. 2022
  • Official Generics Docs: www.typescriptlang.org/docs/h...
    shaky.sh
  • Věda a technologie

Komentáře • 59

  • @andrew-burgess
    @andrew-burgess  Před rokem +7

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

  • @DemPilafian
    @DemPilafian Před rokem +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".*

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

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

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

  • @JosimarBezerra-fx8wb
    @JosimarBezerra-fx8wb Před rokem +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.

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

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

  • @Oxfist
    @Oxfist Před rokem +1

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

  • @TimothySmithDev
    @TimothySmithDev Před rokem +3

    Excellent video, as always

  • @Dgiulian
    @Dgiulian Před rokem

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

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

    Your channel is such a gem

  • @ErShikharChauhan
    @ErShikharChauhan Před rokem +1

    best explanation for generics ever

  • @nicio1393
    @nicio1393 Před rokem

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

  • @ninthsun
    @ninthsun Před rokem

    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!

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

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

  • @mmmike3426
    @mmmike3426 Před rokem +3

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

    • @mmmike3426
      @mmmike3426 Před rokem

      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!

  • @zeeebooo86
    @zeeebooo86 Před rokem

    Fantastic explanation! Bravo man

  • @TheDanthelion
    @TheDanthelion Před 6 měsíci

    Great practical example, really helped clear things up

  • @RayanMADAO
    @RayanMADAO Před 10 měsíci

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

  • @Cypekeh
    @Cypekeh Před 9 měsíci +1

    I like the word "job" now

  • @georgiosmylonas3892
    @georgiosmylonas3892 Před 9 měsíci

    Super helpful! Thanks a lot for this!

  • @HSBTechYT
    @HSBTechYT Před rokem +1

    This was helpful to form a Nice mental modal

  • @dawid_dahl
    @dawid_dahl Před rokem

    Subscribed! Thank you.

  • @SonAyoD
    @SonAyoD Před rokem

    Great video!

  • @kirillvoloshin2065
    @kirillvoloshin2065 Před 10 měsíci

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

  • @mohamadabdelrida2866
    @mohamadabdelrida2866 Před rokem

    The thumbnail is pretty clever

  • @superdev4024
    @superdev4024 Před rokem

    great content. thanks

  • @TricoliciSerghei
    @TricoliciSerghei Před rokem

    Very nice explanation, thank you very much! ;)

  • @Libertoso
    @Libertoso Před rokem

    Thank you very much

  • @domoknows3275
    @domoknows3275 Před 6 měsíci

    great example!

  • @balaganesh3440
    @balaganesh3440 Před rokem

    Your the the best!

  • @Viva-07
    @Viva-07 Před 6 měsíci

    Thank you..nice video

  • @KarimFayed-oy5oo
    @KarimFayed-oy5oo Před rokem

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

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

  • @anatholekaringanire7649
    @anatholekaringanire7649 Před 6 měsíci

    Nice tip

  • @jaemochoco
    @jaemochoco Před rokem

    Thumbnail got me

  • @jonatanramhoj
    @jonatanramhoj Před rokem

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

  • @dechobarca
    @dechobarca Před rokem +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  Před rokem +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: CZcams doesn’t seem to like the hash sign in that link, so maybe copy and paste it instead of clicking?)

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

  • @johnsailor3590
    @johnsailor3590 Před rokem +1

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

  • @fedordostoevskiy4209
    @fedordostoevskiy4209 Před rokem

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

  • @GreggBolinger
    @GreggBolinger Před rokem

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

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

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

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

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

  • @bolt6572
    @bolt6572 Před rokem

    immediately subscribed plus he's cute

  • @andyl.5998
    @andyl.5998 Před rokem +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 Před rokem

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

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

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

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

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

  • @AlexJohnSuarez
    @AlexJohnSuarez Před rokem +3

    You look like spiderman

  • @jessejoseph2643
    @jessejoseph2643 Před 2 měsíci

    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 czcams.com/video/EcCTIExsqmI/video.html. You are welcome.

  • @ricko13
    @ricko13 Před rokem

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

  • @hari9321
    @hari9321 Před 9 měsíci

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