Typescript Generics Tutorial

Поділитися
Вставка
  • Опубліковано 2 жов 2024
  • Learn how to use generics in Typescript. I go over how to use them in your React.js code and create your own in functions, interfaces, and jsx.
    #typescript
    Code: github.com/ben...

    ----
    Follow me online: voidpet.com/be...
    #benawad

КОМЕНТАРІ • 210

  • @sanjarmirakhmedov4016
    @sanjarmirakhmedov4016 5 років тому +199

    One of the best tutorials on Generics I have ever watched. Thank you Ben!

    • @edwingarcia5043
      @edwingarcia5043 4 роки тому +1

      Not for beginner though, while I was watching this I could see the useful parts, but some times I just didn't get it (most parts in React part) ... I will read and fully undertand their full important, I won't give up.

    • @georgestan9563
      @georgestan9563 4 роки тому +7

      @@edwingarcia5043 The generics explanation was very good, you're probably lacking other information needed to understand the code, I recommend revisiting more basic concepts of programming before jumping into this.

    • @foobars3816
      @foobars3816 3 роки тому

      @@georgestan9563 No it was clearly noting to do with the basics of programming and more about him not understanding how React and Formik works. I think he pretty clearly said that in his comment. Which BTW is quite irrelevant for him unless he want's to use either of those, this was just more of showing a practical example for people who are familiar with React and Formik.

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

      He's an asset in the JavaScript community

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

      Was about to say the same thing. No nonsense, really clear and focused!

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

    This is the best generics tutorial on UA-cam

  • @bassam.2023
    @bassam.2023 Рік тому

    Finally I understand generics. Thanks!

  • @kps2642
    @kps2642 5 років тому +61

    This video legit dropped right when I was reading Typescript docs

  • @glzr_io
    @glzr_io 5 років тому +25

    Mad props for making this. Found that the docs were pretty lacking when it came to generics and this really cleared things up. Been using TS for a while now but still found this super helpful

  • @Cognitoman
    @Cognitoman 5 років тому +53

    Think of generics as arguments to functions except your doing types :) ur welcome

    • @bawad
      @bawad  5 років тому +14

      I like that

    • @Cognitoman
      @Cognitoman 5 років тому +7

      Yeah that’s how I think of it anyways

    • @mcaldex
      @mcaldex 3 роки тому +1

      Yes! That's what I thought aswell :)

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

      is there other way to think of it? damn

    • @talhakaraca
      @talhakaraca 3 роки тому

      The point that i couldnt get is why should i use generics while i can basically make type definiton for parameters and returns? Can you help me please

  • @Ascended23
    @Ascended23 4 роки тому

    Great video! I’m so glad that you used example functions other than an “identity” function that just returns the argument. It’s much easier to understand when you can infer how the thing it’s doing is something that could be useful.

  • @Middollo
    @Middollo 4 роки тому

    Best generics explanation i found on youtube so far. Thanks.

  • @swyxTV
    @swyxTV 5 років тому +51

    to return the tuple you can also assert `as const` at the end of that function

    • @ashishmehradotdev
      @ashishmehradotdev 3 роки тому +3

      Yes, that might be another way but the disadvantage will be that typescript will not allowed to push more values in returned array as we declare it as ready-only via const.

    • @jivkojelev3744
      @jivkojelev3744 3 роки тому

      Or u can use keyword readonly.

  • @ashishmehradotdev
    @ashishmehradotdev 3 роки тому

    Can't expect better explanation than this. Thanks Ben.

  • @rafarovina
    @rafarovina 4 роки тому +33

    I can't count how many times I've use a Ben's video to settle a discussion :D Thank you for the all awesome content! Inspiring programming style, using TS at it's best, always!

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

    absolutely brilliant tutorial, thanks!

  • @Cognitoman
    @Cognitoman 5 років тому +16

    By the way ur killing it my dude. Keep grinding your in your way

  • @navazalani6941
    @navazalani6941 3 роки тому +11

    At 19:37, it seems to be some sort of parsing error which mistakes for JSX instead of a TS generic. You don't necessarily have to "extend" something to get the parser to realise it's a TS generic -- a simple would work as well.
    Great video, Ben!

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

      I was running into the exact same error at 4:16 too! Thanks man

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

      Thanks Man

  • @EddyVinck
    @EddyVinck 5 років тому +4

    Great video. Really liked how you included some React as well.

  • @mccall7122
    @mccall7122 2 роки тому +4

    I've been youtubing and googling for over an hour trying to wrap my head around generics. The first 5 minutes of your video did a better job than all the other videos and articles and documentation. Kudos, and thanks!

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

    At 11:50 when he mistakenly wrote boob :)

  • @Ko0lHaNDLuKex
    @Ko0lHaNDLuKex 5 років тому +10

    Really good as always, Ben! I've been using TypeScript for a while now, but I still learnt a lot from this video! Thank you.

  • @datasciencetoday7127
    @datasciencetoday7127 3 роки тому

    i miss these kind of videos @Ben

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

    This was an awesome video sir. Thank you for this

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

    This will take some time to digest.

  • @wojciechniedzwiedz8068
    @wojciechniedzwiedz8068 5 років тому +5

    When you want to use only one generic arg with react FC you can also write it like that:

  • @avimehenwal
    @avimehenwal 3 роки тому

    Wow! amazing ... I just realized its Parametric polymorphism

  • @ikezedev
    @ikezedev 5 років тому +5

    I love this.... Just started rewriting an open source library of mine in typescript and this was just what I needed. Thanks @Ben Awad

  • @vim55k
    @vim55k 4 роки тому

    Thanks. 3 new uses for generics

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

    Thanks man, this really helpful!

  • @umarjaved1489
    @umarjaved1489 3 роки тому +1

    Ben, to get the most out of your videos, one must already be familiar with the concept

  • @HelloThere-xs8ss
    @HelloThere-xs8ss 3 роки тому +1

    Here's the easiest generic:
    let arr = [].
    🤣

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

    ياااخي احبك والله ضحكتني والله تحياتي الك بن عواد هههههههه

  • @andrewmundy
    @andrewmundy 3 роки тому +1

    When using a generic with JSX it'll scream at you if unless you use but you can also say , took me awhile to figure that one out.

  • @charlietaylor9266
    @charlietaylor9266 4 роки тому

    nice one! just what i was looking for.

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

    WOW!!! I could kiss my screen right now!!
    Ben you beautiful mind you!! Thank you, I understand Generics now!

  • @troooooper100
    @troooooper100 4 роки тому

    Love how typed language nazis accept generics. LOL what, types are great but here are generics that allow you to USE ANY TYPE WOAHHH kinda like untyped language?

  • @josephajibodu2377
    @josephajibodu2377 3 роки тому

    Someone from a Java background should have issue with Typescript

  • @ryancox4101
    @ryancox4101 3 роки тому

    The mountainous volleyball wessely search because yak puzzlingly harass into a unknown appeal. reflective, giddy doll

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

    Thanks bro. I was look for interfaces that implement generics just right now

  • @metagamingOficial
    @metagamingOficial 4 роки тому +1

    Thanks bro. This tutorial really helped me out understand generics. I used to look at 's all around a codebase and I was like wtf is this shi*?

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

    really good explain Typescript Generics..!👍
    As a developer we always look for short video but better to explain. you did good job to explain and that what i like about this video.

  • @writegoodcode
    @writegoodcode 3 роки тому +1

    the bob was a typo on purpose xd

  • @raghavsurya4755
    @raghavsurya4755 3 роки тому

    Man, this is amazing stuff! Thank you bro

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

    Aaaand... that's it? xD Man, I was watching others trying to explain it like it was some sort of a rocket science. Thanks!

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

    TypeScript it's much more then react uses... Better you go with classes and other useful examples, not react.

  • @thevividversatilechannel4807

    TypeScript logo has rounded corners, but logo used in the thumbnail does not have so.
    Thank you very much.

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

    At 9:53 / 21:55 you deleted everything within the . and again at 9:52 / 21:55 you delete ', number". how do you do this. i am aware of CMD + Del and OPT + Del

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

    Funnily enough, if we move the object passed on line 33 @ 12:13 into a new variable, and then pass the variable into the fn, the error goes away. Wierd Stuff

  • @mayur9876
    @mayur9876 5 років тому

    This helped me alot. Thanks Ben.

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

    Great content! Btw: what keyboard you use? Any recommendation? Tks

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

    Отличное видео, спасибо!

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

    Typescript is like rolling a turd in glider (the turd being JavaScript)

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

    Thank you so much, for your great Generics tutorial.

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

    This is awesome.

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

    What is the shortcut you did at 9:56? How did you remove everything inside the `` without selecting it?

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

    Thanks for this video it really help and simple to understand typescript generics

  • @xyz-ey7ul
    @xyz-ey7ul 2 роки тому

    dopest video ever. i always come to this when i forget generics. this video alone is 80% typescript

  • @JonathanRintala
    @JonathanRintala 5 років тому +1

    Awesome tutorial 🔥

  • @none_the_less
    @none_the_less 4 роки тому

    Great content. Keep up with the good work.

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

    Ben Awad the boss of coding on youtube and some of the best explanations that I have seen.

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

    This is a good ass tutorial!

  • @flamepotatoes
    @flamepotatoes 3 роки тому

    I just finished a whole project without generics, I wish I watched this video before. Why is this rarely mentioned in Typescript tutorial on youtube?

  • @arpitbharti6245
    @arpitbharti6245 4 роки тому +1

    At 19:43 Line-18, you don't _have_ to write ** instead write it as ** notice the comma after T

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

    What video recording program did you use? Love the code editor view with you in frame

  • @whoamiwhoru
    @whoamiwhoru 3 роки тому +1

    This tutorial cannot be free! Thank you so much :)

  • @Rudenich911
    @Rudenich911 3 роки тому

    Thanks, great explanation

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

    - The best generics tutorial I've ever watched!

  • @serhiikhrapin5024
    @serhiikhrapin5024 3 роки тому

    19:58 is that a common approach to use children in this fashion? It is not obvious for me as for newbie.

  • @yuri4n
    @yuri4n 5 років тому +2

    The video was really quickly but you give the concepts clearly and concise, thanks, man

  • @beyourahi
    @beyourahi 3 роки тому +1

    11:48 when you see it

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

    this the best typescript tutorial video i have ever watched, thank you

  • @dawid_dahl
    @dawid_dahl 4 роки тому +2

    You have the best tutorials on UA-cam. Thanks so much!

  • @NOTHING-en2ue
    @NOTHING-en2ue Рік тому

    very great tutorial, now i understand Generics ❤

  • @devendrajadhav8538
    @devendrajadhav8538 3 роки тому

    how to set generic to @Input and @Output properties please

  • @saadabbasi2063
    @saadabbasi2063 3 роки тому

    Hi,
    Can anybody tell me about the keyboard shortcut he used 9:56
    makeArray(“a”, 5)
    To
    makeArray(“a”, 5)
    Removed

    • @JayTailor45
      @JayTailor45 3 роки тому

      I believe he used dt( to remove ( "dt(" means delete from current cursor position till parenthesis)
      FYI he is using VIM for these fast edits ;)

  • @VectorBandit
    @VectorBandit 3 роки тому

    I just love that you're using Vim.

  • @romanmunar
    @romanmunar 4 роки тому

    I need help, I'm stucked, i couldn't figure out how to take the type that I passed inside a generic at run time, let's say I have a generic, Content, and the ContentType may be Facebook | UA-cam | Reddit... , how would i know which content type is currently being used.
    The example Tab is really similar to my current situation, but i wan't to know what type i've passed inside the Tab generic

  • @gopiloxi
    @gopiloxi 7 місяців тому

    Still relevant in 2024, thanks a lot Ben :)

  • @CoryTheSimmons
    @CoryTheSimmons 4 роки тому

    Just got into Typescript pretty hard about a month ago. Was always confused what the

  • @infiniteeyelashes6729
    @infiniteeyelashes6729 5 років тому +2

    Excellent video, learned a lot.

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

    Thank for the video! Very useful!

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

    11:50 nice

  • @dkumar8037
    @dkumar8037 4 роки тому

    you are looking like kid ,How old are you brother ?

  • @yoking1213
    @yoking1213 5 років тому +1

    Thanks Ben,
    Please keep making videos like these on Typescript

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

    Thank you Mr. Awad for the short, concise, and elegant explanation of generics.

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

    I read few blog posts about generics and thought I am too stupid for that but your explanation made me understand everything I struggled with before. Thank you

  • @Ricardoromero4444
    @Ricardoromero4444 3 роки тому

    When I try to do a generic react component, typescript either screams that the function has no explicit return, or that the function cannot be used as a jsx component. Anyone know how to fix this?

  • @АлексейАлексеевич-ш7ю

    At 8:52 if I pass undefined instead of null, it will also work. Strange typesafety

  • @Vietnamcamping89
    @Vietnamcamping89 4 роки тому +1

    Microsoft is proud of you 😹

  • @rakeshjoshi2306
    @rakeshjoshi2306 5 років тому +1

    This video is really great to start with typescript generics

  • @sonmai3526
    @sonmai3526 3 роки тому

    With C++ background, these are easy :)

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

    Thank you Ben! Now I can refactor my project better.

  • @cesarbuenrostro622
    @cesarbuenrostro622 5 років тому +1

    Thanks dude, I'm studying for a week, but this video clears all the doubts, you're rock!

  • @ARMALON87
    @ARMALON87 3 місяці тому

    Love it, thank you, man!👍

  • @maxtayebwa8987
    @maxtayebwa8987 5 років тому

    Thanks Ben!

  • @kallekula9202
    @kallekula9202 4 роки тому +1

    Your videos are always great. Thanks Ben. You explain everything really well.

  • @shokhbozabdullayev6260
    @shokhbozabdullayev6260 3 роки тому

    15:22 - Such a good expression )

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

    awesome, thx.

  • @dimavoitenko2386
    @dimavoitenko2386 4 роки тому +1

    Thank you! Really clear explanation.

  • @g_dfe1
    @g_dfe1 5 років тому

    Would you consider doing a video on your process for auto generated code that you use?

    • @bawad
      @bawad  5 років тому

      Are you referring to github.com/dotansimha/graphql-code-generator

  • @pyakz6474
    @pyakz6474 3 роки тому

    TypeScript makes me dizzy

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

    Thanks for thie video.

  • @_oo.monkeypox.oo_3344
    @_oo.monkeypox.oo_3344 2 роки тому

    Great way to explain things. Thanks Ben