Typescript Generics | Beginners Tutorial with Examples

  Рет қаралды 16,725

Dave Gray

Dave Gray

Күн бұрын

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
Learn about Typescript Generics in this Typescript beginners tutorial with examples. Typescript Generics allow for type variables which are helpful when we do not know what type will be passed to a TS function, interface, type alias or class.
🚀 Become a full-stack web dev with Zero To Mastery Courses:
- The Complete Web Developer: bit.ly/WebDevMaster
- Master the Coding Interview: bit.ly/FAANGInterview
- Junior to Senior Dev Roadmap: bit.ly/WebDevRoadmap-JrtoSr
🚩 Subscribe ➜ bit.ly/3nGHmNn
📬 Course Updates ➜ courses.davegray.codes/
❓ Questions - Please post them to my Discord ➜ / discord
☕ Buy Me A Coffee ➜ www.buymeacoffee.com/davegray
👇 Follow Me On Social Media:
Github: github.com/gitdagray
Twitter: / yesdavidgray
LinkedIn: / davidagray
🔗 All Resources for this Typescript Course: github.com/gitdagray/typescri...
🔗 Playlist for this Typescript Course: bit.ly/3GcNJ78
Typescript Generics | Beginners Tutorial with Examples
(00:00) Intro
(00:05) Welcome
(00:28) Starter code and set up
(01:33) What are Generics?
(02:02) Basic Example and Syntax
(03:46) isObject example
(05:53) isTrue with keyof Assertion
(11:03) Interface with Generic example
(13:00) Narrowing Generics with Extends
(15:05) Multiple Type Variables & Extends with keyof
(19:43) Class with Generics
📚 Suggested Pre-requisites for this Typescript course:
🔗JavaScript for Beginners full course: • JavaScript Full Course...
📚 Tutorial References:
🔗 Typescript Official Website: www.typescriptlang.org/
🔗 Anders Hejlsberg, Creator of Typescript and C# Interview: dev.to/destrodevshow/typescri...
🔗 Stackoverflow Survey Results: survey.stackoverflow.co/2022/...
🔗 MDN - Static Typing: developer.mozilla.org/en-US/d...
🔗 MDN - Dynamic Typing: developer.mozilla.org/en-US/d...
🔗 MDN - Type Coercion: developer.mozilla.org/en-US/d...
🔗 TS Type Assertions: www.typescriptlang.org/docs/h...
⚙ Web Dev Tools:
🔗 Chrome Browser: www.google.com/chrome/
🔗 Visual Studio Code (VS Code): code.visualstudio.com/
🔗 Node.js & npm: nodejs.org/
🔗 Live Server VS Code Extension: marketplace.visualstudio.com/...
Was this Typescript Generics tutorial helpful? If so, please share. Let me know your thoughts in the comments.
#typescript #generics #tutorial

Пікірлер: 83
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
In this tutorial, we'll learn about Typescript Generics and look at several examples of how to use them. If you have questions, please join my Discord server where I answer questions and you can chat with other web dev students: discord.gg/neKghyefqh
@madhavjha844
@madhavjha844 10 ай бұрын
It was tough for me, maybe I'll have to watch it again. Content was great as always.
@caffeinated-pixels
@caffeinated-pixels Жыл бұрын
I've been using typescript on an almost daily basis for the past five months now and I still find these videos useful. I also like that you explain everything in each video - how to set up the project, how to use VS Code, keyboard shortcuts, etc. A lot of KZbinrs advertise their tutorials as being for beginners but then completely fail to explain fundamental steps and concepts. Great stuff!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful! I really appreciate the feedback! 💯
@aarondiaz2506
@aarondiaz2506 10 ай бұрын
Dave you the man! Best TS tutorial series i have seen on KZbin, from beginner to "advance". It's long but pays off to see it (no more headaches from TS erros 🤣)
@studyingasyouwere
@studyingasyouwere Жыл бұрын
Thanks Dave. I learnt HTML and CSS from your videos from scratch and now I post my own web dev content! Thank you for inspiring me!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Nice work!
@VanPetersonKpoti
@VanPetersonKpoti 4 ай бұрын
I like all your content. They are always top notch and well done. Typescript Generics are not as complicated as we think. Thanks Dave.
@ScriptRaccoon
@ScriptRaccoon Жыл бұрын
That example where the keys are inferred and you get that drop-down is just awesome. I think this showcases very well why TypeScript is not (what I still thought months ago) a burden, but rather a huge help. The compiler (or is it the editor?) tells me right away which values can be passed in. It guides me in the right direction all the time. Someone recently put out a tweet saying something like "Coding in JavaScript after coding with TypeScript for so long feels like driving without a steering wheel".
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Agreed and thanks! Yes, with Microsoft behind both TS and VS Code, the integration and dev experience is excellent. 💯
@sinxenon3181
@sinxenon3181 11 ай бұрын
Amazing explanation! I'm shocked about how great teacher you are! Thanks a lot!!!
@loaiamer7097
@loaiamer7097 Жыл бұрын
Played 20 sec after video posted :D
@oussemabouyahia474
@oussemabouyahia474 5 ай бұрын
I have been a while using react with typescript and I thought that i know everything till I see this video, great content Dave , you are the best developer I ve never seen
@k303k
@k303k 7 ай бұрын
Thanks a million Dave!
@NOTHING-en2ue
@NOTHING-en2ue 11 ай бұрын
very nice tutorial, thanks a lot 💗
@roman_k89
@roman_k89 Жыл бұрын
thanks, Dave! Brilliant as always💙💛
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're very welcome! 💯
@riyadhossain1706
@riyadhossain1706 Жыл бұрын
Thanks a lot for such an awesome Typescript playlist
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@gmjitendra
@gmjitendra Жыл бұрын
Detailed explanation found here over TS Generics. Thanks Dave for this lovely video. :)
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@shahramkhaleh3078
@shahramkhaleh3078 Жыл бұрын
Thanks. for Best typescript tutorial 🌹
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You are welcome 😊
@1conscience0dimension
@1conscience0dimension Жыл бұрын
Super clear and perfect examples
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you!
@sonamohialdin3376
@sonamohialdin3376 Жыл бұрын
Amazing tutorial thank you
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're very welcome!
@regilearn2138
@regilearn2138 Жыл бұрын
Awesome, Eagerly wafting for your REACT with all the hooks/MERN/Redux + TYPESCRIPT ADVANCE real world project. ❤❤LOVE YOU DAVE❤❤
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thanks!
@user-fk2eq9dp2p
@user-fk2eq9dp2p Жыл бұрын
Thank you, Dave
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@harmez7
@harmez7 Жыл бұрын
thank you Mr. Gray
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@fizzdevdesigns5699
@fizzdevdesigns5699 Жыл бұрын
Great series!!! Thank you so much! In the isTrue function, I kind of prefer an 'object' assertion in the object case because it feels more self-explanatory: if (isObj(arg) && !Object.keys(arg as object).length) return {arg, is: false} ... Does it make sense?
@user-te2pe5qm8o
@user-te2pe5qm8o Жыл бұрын
Dave is always there when you need help in the developer journey
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
🙌🚀🚀
@hosamgnaba3205
@hosamgnaba3205 Жыл бұрын
remember to keep striving for progress over perfection, and little progress everyday will go a very long way ... let saying : String[] = ['very','inspiring','saying',':)']... thank you man
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@doongie07
@doongie07 Жыл бұрын
Hey Dave, have you thought about making a cloud computing course?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
I have not but thank you for the request!
@sparshverma1369
@sparshverma1369 Жыл бұрын
The first example reminds of templates in C++ in STL
@adelprog
@adelprog Жыл бұрын
null is an object, thanks for this information
@user-wh6ki2oj3l
@user-wh6ki2oj3l Жыл бұрын
Hi Dave, I hope next you make full tutorial about NextJS with real world project because I like your teaching style
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you for the request and the kind words!
@learn029
@learn029 Жыл бұрын
Can you please also make video on either type for response handling type Left type Right Please make video on this
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
When you say response handling, I think of promises and fetch requests. Is there a link where you specifically saw these names for types? I can name a type anything and generics can also be anything.
@mohamadlakees8318
@mohamadlakees8318 9 ай бұрын
Hay Dave ,i have a question ,in user Array example i understand everything in example except the T[k][ ] ,why you wrote it like this ?
@shubhampatankar1468
@shubhampatankar1468 Жыл бұрын
Pls post video on angular
@harmez7
@harmez7 Жыл бұрын
that's some weird ass syntax right there but thanks to you Dave i had much easier time wrapping my head around it . Bring it on you flipping TS sonofa JS ! aint gonna beat me !
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You got this! 🚀
@skillkrio
@skillkrio Жыл бұрын
Dave can you help me to setup my github. I always keep my notes in github but sometimes that needs to be updated too. I was thinking a clear way to do that. I saw you html repo where you had 10 seperate folder and one readme file inside one single repo. How you did that ? I tried doing that but commit history tracks the entire project but i wanted to have each separate folder to have its own commit history. Can you explain me how can i do that just like yours. seems like repo nesting.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
What I do still has a commit history. I'm not creating branches but instead, just adding new folders to the same "main" branch. You may actually be interested in creating separate branches where each has its own history.
@denmccormik7654
@denmccormik7654 Жыл бұрын
Hello, Dave I've already asked this question on your discord channel, but maybe it will be useful for other people here: Can you explain why we use `as keyof T` on 11:05?? Because for me `arg` is typeof T, not typeof `keys of T` So this is confusing `keyof T` returns union Type, not type describing object, but method `keys` expects object So to simplify it When we pass obj in our function { id:1, name: 2} It should look like Object.keys(arg as ("id" | "name")) But for me it doesn't make sense because arg should be object
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Object.keys() just returns the keys of the object as an array. We are checking to see if the generic data we passed to the function as arg is an object. If it is an object, we pass it to Object.keys() to iterate through the array. However, TS doesn't like that until we tell it arg is viewed as keys here.
@denmccormik7654
@denmccormik7654 Жыл бұрын
@@DaveGrayTeachesCode but how object can be viewed as union type of values? I understand if we use ` arg as {[key:string] : any} ` But why TS accept `arg as `string | string | string`` Keyof T returns string | string | string Arg is object So we tell TS that we pass string in method which expects only object And it works
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
@@denmccormik7654 Object.keys() only returns the keys in an array. I cannot answer _why_ TS does things in a certain way, but this is what TS accepts here.
@denmccormik7654
@denmccormik7654 Жыл бұрын
@@DaveGrayTeachesCode Thank you, Dave. Maybe my question is extra stupid. I need some practice to discover it.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
@@denmccormik7654 no question is stupid. That's how we learn. 💯
@7doors847
@7doors847 Жыл бұрын
Yeah! TS Tuesday 🤘! ( and maybe some tacos too 😂 )
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Right on! 🌮🌮🌮🌮
@abubakarsadiqbilyamin7173
@abubakarsadiqbilyamin7173 Жыл бұрын
Great course. Really informative. I'd like to join the discord server but the link is expired. Could I have a more recent one?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Strange because the link does not expire. I just checked. Here it is: discord.gg/neKghyefqh
@aleksandrpetrov3938
@aleksandrpetrov3938 Жыл бұрын
for some reason I don't have any type Error in the example with Object.keys(arg). But if I put as keyof T, I will get this error: "Conversion of type 'T' to type 'keyof T' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first." Why is that? ))
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Sounds like you have something different from the example code - see the link to the course resources. However, the suggestion you received is talking about a double assertion where you say "as unknown" followed by "as keyof T". It can work in a pinch but no example here should need that.
@kennedydre8074
@kennedydre8074 Жыл бұрын
can't we just use the any type?
@MuhammadSalmanAshraf-hy8gc
@MuhammadSalmanAshraf-hy8gc Жыл бұрын
please write problem statment before any example so that we try by ourself nd then match solution with yours
@ahmedhassan_saver
@ahmedhassan_saver Жыл бұрын
Can you please respond to my Twitter message asking you about coming videos titles in this playlist
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Hello Ahmed, I do not check Twitter messages often. Please join my Discord that I check nearly every day: discord.gg/neKghyefqh
@robertkennedy7772
@robertkennedy7772 Жыл бұрын
promosm
@dzhaniivanov5837
@dzhaniivanov5837 Жыл бұрын
100k
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
🚀🚀
@josippardon8933
@josippardon8933 10 ай бұрын
Despite video saying it is for beginners, it is not. Especially example with usersArray.
@siddiqahmed3274
@siddiqahmed3274 10 ай бұрын
You should follow the complete series. As this is almost the end of the series you cannot expect to grab all without practicing
@josippardon8933
@josippardon8933 10 ай бұрын
@@siddiqahmed3274 I have gone through every video in series till this one
@siddiqahmed3274
@siddiqahmed3274 10 ай бұрын
@@josippardon8933 okay that's good. I hope you have practiced that as well. This is is a advance concept so if you don't grasp it, leave it for now and when you need it you can come back. On the second note if you try the examples yourself, breaking it, you will get it.
@hamzabonga7884
@hamzabonga7884 Жыл бұрын
This is not a beginner friendly I have not understand a single example
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Generics can be difficult to understand. Did you complete the previous 7 chapters before attempting this video? That will help.
@hamzabonga7884
@hamzabonga7884 Жыл бұрын
@@DaveGrayTeachesCode yes I did complete previous 7 chapters they are completely understandable
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
@@hamzabonga7884 Glad to hear I helped with those chapters. Maybe seek some other sources to understand generics if this video didn't help you.
@user-xn6li8ij2j
@user-xn6li8ij2j 7 ай бұрын
Example is too long and complex for beginners. Should show how the 'T' works first.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 7 ай бұрын
I cannot please everyone. Please feel free to make your own tutorial the way you would like it.
@Imam.Mehdi_
@Imam.Mehdi_ 6 ай бұрын
​​@@DaveGrayTeachesCode Yes ,,, instead of making generics easier you made it very difficult.... You teaching style is good ...in this vide you just messed the generics in loops and isArray and null is object etc...which are difficult for beginners... I am not complaining...I am just a student watching your video,,, it was hard for as I came here to learn generics and got stuck in loops and null is Array etc
Typescript Utility Types | TS Beginners Tutorial
25:11
Dave Gray
Рет қаралды 11 М.
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 123 М.
Универ. 13 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:07:11
Комедии 2023
Рет қаралды 4 МЛН
Final muy inesperado 🥹
00:48
Juan De Dios Pantoja
Рет қаралды 10 МЛН
Most React devs don’t understand generic components
5:43
Matt Pocock
Рет қаралды 46 М.
TypeScript Generics: All You Need to Know
7:52
Software Developer Diaries
Рет қаралды 4,5 М.
Is THIS Python's MOST Underrated Operator? (Walrus Operator)
5:45
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 57 М.
React Typescript Tutorial for Beginners
28:24
Dave Gray
Рет қаралды 73 М.
No BS TS #7 - Generics in Typescript
8:20
Jack Herrington
Рет қаралды 38 М.
Typescript Basic Types - Beginners Lesson
16:16
Dave Gray
Рет қаралды 18 М.
The Magic of TypeScript Decorators
9:51
Fireship
Рет қаралды 249 М.
Typescript Generics Tutorial
21:56
Ben Awad
Рет қаралды 218 М.