How to use generics in TypeScript

Поділитися
Вставка

КОМЕНТАРІ • 62

  • @andrew-burgess
    @andrew-burgess  2 роки тому +7

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

  • @DemPilafian
    @DemPilafian Рік тому +7

    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 2 роки тому +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 2 роки тому +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 2 роки тому +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 Рік тому +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 Рік тому

    UA-cam 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!

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

    Your channel is such a gem

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

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

  • @mmmikem
    @mmmikem 2 роки тому +3

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

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

      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!

  • @TimothySmithDev
    @TimothySmithDev 2 роки тому +3

    Excellent video, as always

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

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

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

    best explanation for generics ever

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

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

  • @TheDanthelion
    @TheDanthelion 11 місяців тому

    Great practical example, really helped clear things up

  • @jrnxf
    @jrnxf 2 місяці тому

    best yt thumbnail in a long time lmao

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

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

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

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

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

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

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

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

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

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

    The thumbnail is pretty clever

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

    I like the word "job" now

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

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

    This was helpful to form a Nice mental modal

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

    Super helpful! Thanks a lot for this!

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

    Thank you very much

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

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

    Fantastic explanation! Bravo man

  • @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 2 роки тому +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  2 роки тому +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: UA-cam doesn’t seem to like the hash sign in that link, so maybe copy and paste it instead of clicking?)

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

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

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

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

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

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

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

    Thumbnail got me

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

    Subscribed! Thank you.

  • @domoknows3275
    @domoknows3275 11 місяців тому

    great example!

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

    great content. thanks

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

    Very nice explanation, thank you very much! ;)

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

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

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

    Your the the best!

  • @Viva-07
    @Viva-07 11 місяців тому

    Thank you..nice video

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

    Great video!

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

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

  • @kashmirtechtv2948
    @kashmirtechtv2948 20 днів тому

    Not making the mental model still.. Video is very good at the start

  • @anatholekaringanire7649
    @anatholekaringanire7649 11 місяців тому

    Nice tip

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

    immediately subscribed plus he's cute

  • @hypealex
    @hypealex 2 роки тому +3

    You look like spiderman

  • @anton4488
    @anton4488 4 місяці тому

    Seeing “any” makes me very uncomfortable. Clearly this is a tutorial for beginners. So they should be discouraged as much as possible from using “any”.

  • @jessejoseph2643
    @jessejoseph2643 8 місяців тому

    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 ua-cam.com/video/EcCTIExsqmI/v-deo.html. You are welcome.

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

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

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

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