Learn TypeScript Generics In 13 Minutes

Поделиться
HTML-код
  • Опубликовано: 6 июн 2024
  • TypeScript Simplified Course: courses.webdevsimplified.com/...
    By far one of the hardest TypeScript concepts to understand when first learning TypeScript is generics. They introduce new syntax, new logic, and a new way of thinking all at the same time which is tough to comprehend. In this video I will be breaking down everything you need to know about generics (including advanced generic features).
    📚 Materials/References:
    TypeScript Simplified Course: courses.webdevsimplified.com/...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:49 - When To Use Generics
    02:04 - Generic Function Basics
    06:40 - Generic Type Basics
    09:24 - Advanced Generic Features
    #TypeScript #WDS #TypeScriptGeneric

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

  • @Oliver-rh5bv
    @Oliver-rh5bv Месяц назад +11

    Very often it feels like - yes its totally clear and so easy - when watching videos like this.
    Then we go back to work and in most cases our problems are more complex and they still need some time to get around that barrier in the head and apply what we just have learned.
    I tought myself the generic stuff by using it in my daily work and I still keep watching videos like this to learn about the very edgew cases - the smart little things I missed in the documentation documents in the web.

  • @newgem6825
    @newgem6825 4 месяца назад +13

    Whenever I find it very hard to grasp a concept, I run to this channel. You are the GOAT

  • @WebSurfingIsMyPastime
    @WebSurfingIsMyPastime 4 месяца назад +1

    I needed more info on generics and I had already signed up for the react course on webdev. I'm glad I purchased the course on typescript. The extra info in your course was exactly what I needed to guide me through a basic understanding of generics.

  • @waseemtahir6740
    @waseemtahir6740 2 месяца назад +6

    This is shortest and the best explanation of generics in TS. Thanks for this Kyle.

  • @lauragreen3132
    @lauragreen3132 3 месяца назад +2

    I was baffled by Generics in the docs & other tutorials. This is incredibly helpful and clear. Thank you.

  • @Martin97perussini
    @Martin97perussini 5 месяцев назад +74

    Excellent, you honored the "simplified" to your name with this video, really easy to understand this way

  • @gyros9162
    @gyros9162 5 месяцев назад +10

    This is the shortest and best explanation generic types and functions I've ever saw

  • @fabianpetersen2452
    @fabianpetersen2452 5 месяцев назад +2

    I'm currently learning Typescript and struggled a lot getting used to the syntax and trying to figure out what must be passed in to remove all these errors even though the code is correct with plain react/javascript. Thanks Kyle, this will assist me tremendously going forward.

  • @felistusobieze8951
    @felistusobieze8951 5 месяцев назад +1

    I enjoyed this. Thanks for clarifying. I actually use this but never knew I was implementing a generic type system.
    Learnt new stuff today.

  • @user-xf9rz2xy6d
    @user-xf9rz2xy6d 5 месяцев назад +5

    Really need your nextjs course, Kyle!!!

  • @theisoj
    @theisoj 5 месяцев назад +3

    Great video so far! Thanks Kyle as always! 👍

  • @sergiom.954
    @sergiom.954 5 месяцев назад

    Great video! 👏I have been working with typescript for 3 years (angular) and I didn't even know this generic types possibilities.

  • @khawajamohsin7194
    @khawajamohsin7194 5 месяцев назад

    I just realised how much i needed this video. Good work.

  • @caioveb
    @caioveb 2 месяца назад

    The best drops of knowledge in video I ever seen! Congratulations, man! And thank you very very much

  • @mondirhallouli
    @mondirhallouli 5 месяцев назад

    great video! finally got a better idea on how generics work. I had the basic notion but never understood them like this. They are kinda placeholders for a type(s).

  • @srleom
    @srleom 19 дней назад

    Simply the best explanation of TypeScript generics out there. Kyle, you're a legend. Thanks for making these videos 🙏🏻

  • @PrabhuKripaHai
    @PrabhuKripaHai 5 месяцев назад

    Nicely explained, it just fit into my mind. Thanks.😊

  • @user-yb3cj7rg7q
    @user-yb3cj7rg7q 5 месяцев назад

    Great tutorial. Your explanation is awesome.

  • @pakistanigymers2k22
    @pakistanigymers2k22 5 месяцев назад

    Loved your explanation!!! 😁

  • @SirojiddinMirzayev-zy3pl
    @SirojiddinMirzayev-zy3pl 12 дней назад

    You are the one of the teacher who I know in RUclips , I did not understand until I watched this video why we needs typescript

  •  5 месяцев назад

    You really do the web dev simplified!!!😀

  • @secret786786
    @secret786786 4 месяца назад

    Amazing video... Really helpful!!

  • @anruntxd8711
    @anruntxd8711 19 дней назад

    Thank you so much, your explanation was really easy and helpful!

  • @ClaytonBoyle
    @ClaytonBoyle 2 месяца назад

    Just an amazing 12 minutes of content. Thank you so much

  • @miladevs
    @miladevs 5 дней назад

    This video is a great introduction to generics! Generics can be a confusing topic, but this video breaks it down into clear and easy-to-understand concepts. I especially liked the way he showed how to use generics with API responses. This is a common use case for generics, and it was helpful to see how to create a generic API response type that can be reused for different API endpoints.

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

    so amazing! it's really easy to understand 😄

  • @rEmIxKoOL
    @rEmIxKoOL 4 месяца назад

    Amazing! Clear and helpful

  • @romaniaplus
    @romaniaplus 5 месяцев назад

    Love to see you simplifying the crazy webdev today everything build on top of everything. This mess starts to kill me.

  • @therealj4282
    @therealj4282 2 месяца назад

    Nice clear explanation - so many people get tied up in knots trying to explain generics.

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

    best webDev channel. Thanks)

  • @Belgabor
    @Belgabor 5 месяцев назад

    Very good introduction. One thing I would recommend for people just starting to learn this, do not even start naming generic parameters with single letters, be expressive. You may start out with a simple type where it's clear what 'T' is, but after a few weeks it has T, R, E, O and W. And a month later you have no idea any more what each of the letters are supposed to mean.

  • @thedrew6905
    @thedrew6905 26 дней назад

    short and concise, i always come back to refresh my memory

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

    Thank you very much for always simplifying very well this kind of typescript concepts :D

  • @anotherhumanLingLing
    @anotherhumanLingLing 14 дней назад

    Wow, this is the first time I feel like I understand what Generics type is. BIG THANK YOU!

  • @Matiast9477
    @Matiast9477 5 месяцев назад

    This was great, now I need to understand when you get to that point where you see stuff like T extende keyof K or something like that

  •  5 месяцев назад

    thank you a much, I love your courses

  • @hillelgarcia5140
    @hillelgarcia5140 5 дней назад

    very good stuff, thanks!

  • @MrVipulLal
    @MrVipulLal 5 месяцев назад

    Brilliant video, as usual

  • @AtaurRahman-vm1uz
    @AtaurRahman-vm1uz Месяц назад +2

    Probably the best video for generics in youtube. Hats of man.

    • @true227
      @true227 23 дня назад

      is it a joke?

    • @AtaurRahman-vm1uz
      @AtaurRahman-vm1uz 12 дней назад

      @@true227 Why do you think its a joke? Simple explanation.

  • @Amanrauniyar-dm7in
    @Amanrauniyar-dm7in 3 месяца назад

    brother your next level thankyou for all these

  • @david.thomas.108
    @david.thomas.108 Месяц назад

    Super informative video thanks 👍

  • @user-nc6ho6dj3i
    @user-nc6ho6dj3i 20 дней назад

    Thank you so much, I really appreciate it :)

  • @LeVanHienFEFPLHN
    @LeVanHienFEFPLHN 2 месяца назад

    Great video, thanks for kyle

  • @shreyas12321
    @shreyas12321 22 дня назад

    Cool thumbnail. That was the exact emotions I was going through before seeing this video :) Thank you for the video

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

    Very helpful, Thanks

  • @alexanderkomanov4151
    @alexanderkomanov4151 5 месяцев назад

    Thanks a lot for this!

  • @RandikaTechWork
    @RandikaTechWork 2 месяца назад

    Rally good one .. Keep it up..!!

  • @tubenzr
    @tubenzr 5 месяцев назад

    long time no see Kyle, as always great content. 👍👍👍

  • @leobertobittencourtfilho3036
    @leobertobittencourtfilho3036 Месяц назад

    Goodness me, and you're not even cutting the video every time! Cheers, mate!

  • @crvlwanek
    @crvlwanek 5 месяцев назад +47

    The type system in TypeScript is so much more expressive than other languages. It has great inferencing, keyof for derived types, even ternaries for narrowing/filtering types

    • @arshiagholami7611
      @arshiagholami7611 5 месяцев назад +10

      You need an expressive type system to support types in a dynamic scripting language.

    • @oscarljimenez5717
      @oscarljimenez5717 5 месяцев назад +1

      @@arshiagholami7611actually not, for example python type system is very basic.

    • @arshiagholami7611
      @arshiagholami7611 5 месяцев назад

      @@oscarljimenez5717 python doesn't have a type system. it's a type hint for your IDE.

    • @ElektrykFlaaj
      @ElektrykFlaaj 5 месяцев назад +5

      @@arshiagholami7611 Rust has even more expressive type system than TS, and it's a static typed language

    • @arshiagholami7611
      @arshiagholami7611 5 месяцев назад

      @@ElektrykFlaaj no it doesn't, i've coded in both languages for years and I can tell you typescript's type system is way more complex for obvious reasons. I love rust tho 😇

  • @dukeselwood
    @dukeselwood 5 месяцев назад

    Great video!

  • @naturevibesvlogs810
    @naturevibesvlogs810 2 месяца назад

    Easy explanation ,Simplified the explanation of generic type easy to understand.

  • @abhidadhaniya5701
    @abhidadhaniya5701 5 месяцев назад

    Great video kyle, Please cover keywords like Infer, As and Satisfy in Typescript. Thank you.

  • @YusufxonToshkandiy
    @YusufxonToshkandiy 2 месяца назад +1

    perfect , and thanks bro

  • @pro100tom
    @pro100tom 5 месяцев назад

    Man, after watching all kinds of your videos I am now convinced you are the one to explain the monads!

    • @fifty-plus
      @fifty-plus 5 месяцев назад

      Monads are easy but often over complicated. ruclips.net/user/shortsC2-ljnsckrs?si=cHTDnp5xmdRE_b9f

  • @_WyreZ
    @_WyreZ 5 месяцев назад

    So good. Ty!

  • @gunjan4075
    @gunjan4075 5 месяцев назад +3

    This is super useful for writing clean Typescript code. Thanks for sharing!

  • @norbusherpa3665
    @norbusherpa3665 Месяц назад

    it was so fun to watch this

  • @SlickStatus
    @SlickStatus 5 месяцев назад

    Very helpful ❤

  • @Kat8dev
    @Kat8dev 5 месяцев назад

    really good explanation

  • @theidanking
    @theidanking 4 месяца назад

    Thank you! You know how to explain

  • @StephenRayner
    @StephenRayner 5 месяцев назад

    Already good with generics, this is a decent coverage of them.

  • @StephenMoreira
    @StephenMoreira 4 месяца назад

    Great content.

  • @javadbagheri9921
    @javadbagheri9921 5 месяцев назад

    Amazing ❤

  • @d-princecoder4031
    @d-princecoder4031 2 месяца назад

    great simplification

  • @iqbalibrahim4713
    @iqbalibrahim4713 5 месяцев назад

    You sir, are a hero

  • @DEV_XO
    @DEV_XO 5 месяцев назад

    Amazing!

  • @MagicPants647
    @MagicPants647 5 месяцев назад +2

    I like thinkin of generics as just a way to pass in types to a function or another type. kinda like how you’d pass in parameters to a function.

  • @Ham.AI.
    @Ham.AI. 5 месяцев назад

    it's been a long time since I've seen you ❤

  • @deepkfiz7121
    @deepkfiz7121 2 месяца назад

    thank you!!!!!

  • @NazwaBricksZajeta
    @NazwaBricksZajeta Месяц назад

    Thank you for clarifying this!

  • @true_visual
    @true_visual 5 месяцев назад

    Thank you very very much for this tutorial! Can you please make a video about differences between Generics and Interfaces? I am pretty new and if someone would ask me to define a type of something, my mind thinks "Interface" at once!

  • @_neuromanser_
    @_neuromanser_ 5 месяцев назад

    Never used this, but it seems useful.

  • @gageracer
    @gageracer 5 месяцев назад +1

    bro, you just explained it so well! Now I don't hate generics that much. My only issue was that for some reason you need to put generics between the name and the parenthesis. I was trying to use it for svelte 5 props and was doing: let {name} = $props() and had no idea why it wasn't working. fyi you need to do it $props()

    • @joe-skeen
      @joe-skeen 5 месяцев назад +4

      I think that choice came from other C-family languages that also use the same syntax for generics, like C# and Java to make it more approachable and intuitive for those developers. But for someone that hasn't worked with strongly typed languages in the past, they are understandably intimidating

  • @adityamittal4357
    @adityamittal4357 18 дней назад

    Thanks a lot

  • @bencipherx
    @bencipherx Месяц назад

    I have not started watching this but when I saw the video, I said if I don’t understand this concept after watching your videos then I can never understand it from anywhere else. Already seeing positive comments so 😅fingers crossed 🤞

  • @ahsanimam5243
    @ahsanimam5243 День назад

    Hey @kyle,
    That is an awesome video.
    I wanted to point something in your last example regarding object. The value of new Date() would resolve to `object` so that might not work well. I think a more robust definition for an object would be Record or something along those lines. What are your thoughts?
    And thanks once again for another great video.

  • @fuadagayev-mg7fk
    @fuadagayev-mg7fk 2 месяца назад

    Best Generics explain one of!

  • @rohitpandey4411
    @rohitpandey4411 5 месяцев назад

    you are awesome😇

  • @mukullataroy2195
    @mukullataroy2195 5 месяцев назад +90

    Ahh generics.... My worst enemy

  • @dinguskhan8883
    @dinguskhan8883 5 месяцев назад

    Would love to see a video about what stuff makes typescript’s inference not work

  • @assk477
    @assk477 2 месяца назад

    Verry nice

  • @renishcharaniya6532
    @renishcharaniya6532 5 месяцев назад

    Which extension are you using? the one when you Hover shows the ts types passed.

  • @ms.crawford1335
    @ms.crawford1335 5 месяцев назад

    Brilliant

  • @abnow1998
    @abnow1998 5 месяцев назад

    Awesome

  • @user-du2ru7gc4s
    @user-du2ru7gc4s 5 месяцев назад

    Always different ❤

  • @fifty-plus
    @fifty-plus 5 месяцев назад +10

    Its a better practice to name your generic arguments prefixed with T, like TModel, TRequest, etc. like we do in C#. Yes it's Hungarian-like but it works well to distinguish generic arguments while keeping them explicit and descriptive.

    • @ivan.jeremic
      @ivan.jeremic 5 месяцев назад +1

      Hungarian-like. What does that even mean?

    • @fifty-plus
      @fifty-plus 5 месяцев назад +2

      @@ivan.jeremic Hungarian notation.

    • @IannoOfAlgodoo
      @IannoOfAlgodoo 5 месяцев назад

      Marking your type in TS with T, makes code messy IMO.

  • @themightysapien
    @themightysapien 5 месяцев назад

    You are a G.

  • @Luke_UPPX
    @Luke_UPPX 5 месяцев назад

    thanks

  • @armagananteplioglu9031
    @armagananteplioglu9031 4 месяца назад

    This was a great explanation thank you very much!

  • @tjohns92109
    @tjohns92109 3 месяца назад +2

    Is Typescript really necessary? I have not wrapped my head around why there is a use to make code more complex just to make sure you are using the correct type of variables. I mean... I know what my variables are intended to be when I create them and when I use them later. So I'm not sure of the whole point of TS.

    • @lucasbezerra649
      @lucasbezerra649 Месяц назад

      JSDocs is a good alternative if you feel that way

  •  4 месяца назад

    nice content , I wish you sell your ts course cheaper hence we could buy it my friend 😂

  • @tomasburian6550
    @tomasburian6550 5 месяцев назад +2

    Top. I'm to this day shocked how little attention people give to TS given that we use it everywhere in the professional world. Learning as much of it as possible can make a developer go a long way. The same goes for writing tests.

  • @kylesandstrom853
    @kylesandstrom853 Месяц назад

    Great video and explanation as always, I really like your video tutorials... but TS still feels like overkill in most use cases I've seen.

  • @benjaoliva3383
    @benjaoliva3383 5 месяцев назад +1

    Great video!! Just one question about the last part, is there a way to merge the generic default type with the passed one ? Like Data will always be an abject with status key at least, but i pass a type on variable definition that is an object with name so the variable should have a data key with object containing status and name as value. Is that possible ?

    • @im-essi
      @im-essi 5 месяцев назад

      type ApiResponse = { status: number } & T
      where T is some object type like { name: string } or whatever, is one way, if I understood you correctly. Should then always have status, and add whatever fields else you want.

    • @benjaoliva3383
      @benjaoliva3383 5 месяцев назад

      @@im-essi hmm I see. Chat GPT gave me this:
      type DefaultData = {
      status: string;
      };
      type CustomData = {
      data: DefaultData & T;
      };
      // Example usage
      const myVariable: CustomData = {
      data: {
      status: "success",
      name: "John",
      },
      };
      But here he creates a new type for doing that in place of using the type passed at variable definition. I like more your approach, thanks!

    • @drprdcts
      @drprdcts 5 месяцев назад +3

      Yes, & is the operator for union (this AND that), and | is the operator for intersection (either-or)

    • @joe-skeen
      @joe-skeen 5 месяцев назад +1

      Or just use extends:

  • @mosescosme8629
    @mosescosme8629 4 месяца назад

    My favorite part of this video was learning that I've already been using Generics everywhere and had no idea that's what they were called.

  • @jasonlough6640
    @jasonlough6640 5 месяцев назад

    At 4:02, you pass to tell it what the generic value passed in should be. Makes sense.
    At 4:37, you seem to be passing in... the return type? querySelector, now input is of type HTMLInputElement.
    so which is it, the type passed in or a return type?

    • @kenichiwaaa
      @kenichiwaaa 5 месяцев назад +2

      If you pause at 4:42, you can see that the generic is placed after the semicolon, meaning the return type.
      At 4:02, Kyle specifically use the generic for the parameter passed in.
      So it can be both. It is just a matter of how and where you define it

    • @LittleLily_
      @LittleLily_ 4 месяца назад +1

      The generic type can be used anywhere in the function, either in the input arguments, the return type, or even both. For example:
      function foo(a: T) { return "foo" }
      function bar(a: T): T { return a }
      function getFoo(obj: { foo: T }): T { return obj.foo }

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

    i wish you were a private tutor!

  • @AkioJunichiro
    @AkioJunichiro 5 месяцев назад

    Another generics use is to work with interface, then you can pass any type that respect at minimum the interface structure

  • @superuser8636
    @superuser8636 5 месяцев назад

    Coming from C++, I love everything about TypeScript