Learn TypeScript Generics In 13 Minutes

  Рет қаралды 191,965

Web Dev Simplified

Web Dev Simplified

Күн бұрын

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

Пікірлер: 206
@Martin97perussini
@Martin97perussini 4 ай бұрын
Excellent, you honored the "simplified" to your name with this video, really easy to understand this way
@AtaurRahman-vm1uz
@AtaurRahman-vm1uz 8 күн бұрын
Probably the best video for generics in youtube. Hats of man.
@WebSurfingIsMyPastime
@WebSurfingIsMyPastime 3 ай бұрын
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.
@newgem6825
@newgem6825 3 ай бұрын
Whenever I find it very hard to grasp a concept, I run to this channel. You are the GOAT
@fabianpetersen2452
@fabianpetersen2452 4 ай бұрын
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 4 ай бұрын
I enjoyed this. Thanks for clarifying. I actually use this but never knew I was implementing a generic type system. Learnt new stuff today.
@waseemtahir6740
@waseemtahir6740 Ай бұрын
This is shortest and the best explanation of generics in TS. Thanks for this Kyle.
@theisoj
@theisoj 4 ай бұрын
Great video so far! Thanks Kyle as always! 👍
@mondirhallouli
@mondirhallouli 4 ай бұрын
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).
@sergiom.954
@sergiom.954 4 ай бұрын
Great video! 👏I have been working with typescript for 3 years (angular) and I didn't even know this generic types possibilities.
@lauragreen3132
@lauragreen3132 2 ай бұрын
I was baffled by Generics in the docs & other tutorials. This is incredibly helpful and clear. Thank you.
@khawajamohsin7194
@khawajamohsin7194 4 ай бұрын
I just realised how much i needed this video. Good work.
@gyros9162
@gyros9162 4 ай бұрын
This is the shortest and best explanation generic types and functions I've ever saw
@gheorghitacristea5750
@gheorghitacristea5750 13 күн бұрын
Shortest, you sure?
@PrabhuKripaHai
@PrabhuKripaHai 4 ай бұрын
Nicely explained, it just fit into my mind. Thanks.😊
@user-yb3cj7rg7q
@user-yb3cj7rg7q 4 ай бұрын
Great tutorial. Your explanation is awesome.
@caioveb
@caioveb Ай бұрын
The best drops of knowledge in video I ever seen! Congratulations, man! And thank you very very much
@Matiast9477
@Matiast9477 4 ай бұрын
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
@user-xf9rz2xy6d
@user-xf9rz2xy6d 4 ай бұрын
Really need your nextjs course, Kyle!!!
@crvlwanek
@crvlwanek 4 ай бұрын
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 4 ай бұрын
You need an expressive type system to support types in a dynamic scripting language.
@oscarljimenez5717
@oscarljimenez5717 4 ай бұрын
@@arshiagholami7611actually not, for example python type system is very basic.
@arshiagholami7611
@arshiagholami7611 4 ай бұрын
@@oscarljimenez5717 python doesn't have a type system. it's a type hint for your IDE.
@ElektrykFlaaj
@ElektrykFlaaj 4 ай бұрын
@@arshiagholami7611 Rust has even more expressive type system than TS, and it's a static typed language
@arshiagholami7611
@arshiagholami7611 4 ай бұрын
@@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 😇
@pakistanigymers2k22
@pakistanigymers2k22 4 ай бұрын
Loved your explanation!!! 😁
@jattoritittor8342
@jattoritittor8342 2 ай бұрын
Thank you very much for always simplifying very well this kind of typescript concepts :D
@magorzatagajlusz9170
@magorzatagajlusz9170 Ай бұрын
so amazing! it's really easy to understand 😄
@ClaytonBoyle
@ClaytonBoyle Ай бұрын
Just an amazing 12 minutes of content. Thank you so much
@MartinKonicek
@MartinKonicek 4 ай бұрын
thank you a much, I love your courses
@secret786786
@secret786786 3 ай бұрын
Amazing video... Really helpful!!
@abhidadhaniya5701
@abhidadhaniya5701 4 ай бұрын
Great video kyle, Please cover keywords like Infer, As and Satisfy in Typescript. Thank you.
@romaniaplus
@romaniaplus 4 ай бұрын
Love to see you simplifying the crazy webdev today everything build on top of everything. This mess starts to kill me.
@Amanrauniyar-dm7in
@Amanrauniyar-dm7in 2 ай бұрын
brother your next level thankyou for all these
@rEmIxKoOL
@rEmIxKoOL 3 ай бұрын
Amazing! Clear and helpful
@tubenzr
@tubenzr 4 ай бұрын
long time no see Kyle, as always great content. 👍👍👍
@MrVipulLal
@MrVipulLal 4 ай бұрын
Brilliant video, as usual
@therealj4282
@therealj4282 Ай бұрын
Nice clear explanation - so many people get tied up in knots trying to explain generics.
@HectorGiudatto
@HectorGiudatto 4 ай бұрын
You really do the web dev simplified!!!😀
@bencipherx
@bencipherx 23 күн бұрын
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 🤞
@alexanderkomanov4151
@alexanderkomanov4151 4 ай бұрын
Thanks a lot for this!
@eduardherasym689
@eduardherasym689 2 ай бұрын
best webDev channel. Thanks)
@Belgabor
@Belgabor 4 ай бұрын
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.
@MagicPants647
@MagicPants647 4 ай бұрын
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.
@david.thomas.108
@david.thomas.108 18 күн бұрын
Super informative video thanks 👍
@RandikaTechWork
@RandikaTechWork Ай бұрын
Rally good one .. Keep it up..!!
@true_visual
@true_visual 4 ай бұрын
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!
@gunjan4075
@gunjan4075 4 ай бұрын
This is super useful for writing clean Typescript code. Thanks for sharing!
@YusufxonToshkandiy
@YusufxonToshkandiy Ай бұрын
perfect , and thanks bro
@ehsanset6709
@ehsanset6709 2 ай бұрын
Very helpful, Thanks
@SlickStatus
@SlickStatus 4 ай бұрын
Very helpful ❤
@theidanking
@theidanking 3 ай бұрын
Thank you! You know how to explain
@Kat8dev
@Kat8dev 3 ай бұрын
really good explanation
@javadbagheri9921
@javadbagheri9921 4 ай бұрын
Amazing ❤
@naturevibesvlogs810
@naturevibesvlogs810 Ай бұрын
Easy explanation ,Simplified the explanation of generic type easy to understand.
@_WyreZ
@_WyreZ 4 ай бұрын
So good. Ty!
@pro100tom
@pro100tom 4 ай бұрын
Man, after watching all kinds of your videos I am now convinced you are the one to explain the monads!
@fifty-plus
@fifty-plus 4 ай бұрын
Monads are easy but often over complicated. kzbin.infoC2-ljnsckrs?si=cHTDnp5xmdRE_b9f
@LeVanHienFEFPLHN
@LeVanHienFEFPLHN 27 күн бұрын
Great video, thanks for kyle
@fifty-plus
@fifty-plus 4 ай бұрын
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 4 ай бұрын
Hungarian-like. What does that even mean?
@fifty-plus
@fifty-plus 4 ай бұрын
@@ivan.jeremic Hungarian notation.
@IannoOfAlgodoo
@IannoOfAlgodoo 4 ай бұрын
Marking your type in TS with T, makes code messy IMO.
@StephenRayner
@StephenRayner 4 ай бұрын
Already good with generics, this is a decent coverage of them.
@dukeselwood
@dukeselwood 4 ай бұрын
Great video!
@Hamza.dev-AI
@Hamza.dev-AI 4 ай бұрын
it's been a long time since I've seen you ❤
@gageracer
@gageracer 4 ай бұрын
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 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
@iqbalibrahim4713
@iqbalibrahim4713 4 ай бұрын
You sir, are a hero
@StephenMoreira
@StephenMoreira 3 ай бұрын
Great content.
@DEV_XO
@DEV_XO 4 ай бұрын
Amazing!
@norbusherpa3665
@norbusherpa3665 9 күн бұрын
it was so fun to watch this
@d-princecoder4031
@d-princecoder4031 Ай бұрын
great simplification
@deepkfiz7121
@deepkfiz7121 Ай бұрын
thank you!!!!!
@dinguskhan8883
@dinguskhan8883 4 ай бұрын
Would love to see a video about what stuff makes typescript’s inference not work
@NazwaBricksZajeta
@NazwaBricksZajeta 22 күн бұрын
Thank you for clarifying this!
@_neuromanser_
@_neuromanser_ 4 ай бұрын
Never used this, but it seems useful.
@fuadagayev-mg7fk
@fuadagayev-mg7fk Ай бұрын
Best Generics explain one of!
@rohitpandey4411
@rohitpandey4411 4 ай бұрын
you are awesome😇
@danilomourelle
@danilomourelle 4 ай бұрын
I don't know man. I would say that it gonna use extends a lot when using generics... Every time I neede an explicit generic, half of time is with extends... Great video by the way...
@kylesandstrom853
@kylesandstrom853 21 күн бұрын
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.
@edisonj9664
@edisonj9664 Күн бұрын
Typescript generics explained well
@armagananteplioglu9031
@armagananteplioglu9031 3 ай бұрын
This was a great explanation thank you very much!
@Cognitoman
@Cognitoman Ай бұрын
Think of generics like functions for types, the generic takes a type as an argument. Once you think of it like that… it’s easy
@mosescosme8629
@mosescosme8629 3 ай бұрын
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.
@mukullataroy2195
@mukullataroy2195 4 ай бұрын
Ahh generics.... My worst enemy
@joe-skeen
@joe-skeen 4 ай бұрын
Right next to regular expressions, no doubt 😉
@yohanneskindu8794
@yohanneskindu8794 4 ай бұрын
​@@joe-skeenchatgpt used to do it's magic for me on regex
@elixirdeveloper6673
@elixirdeveloper6673 3 ай бұрын
I feel u
@unleash-gamplay
@unleash-gamplay 3 ай бұрын
Same to you
@elixirdeveloper6673
@elixirdeveloper6673 3 ай бұрын
@@unleash-gamplay dw bro practice makes perfect
@assk477
@assk477 Ай бұрын
Verry nice
@abnow1998
@abnow1998 4 ай бұрын
Awesome
@ms.crawford1335
@ms.crawford1335 3 ай бұрын
Brilliant
@user-du2ru7gc4s
@user-du2ru7gc4s 4 ай бұрын
Always different ❤
@Luke_UPPX
@Luke_UPPX 4 ай бұрын
thanks
@renishcharaniya6532
@renishcharaniya6532 4 ай бұрын
Which extension are you using? the one when you Hover shows the ts types passed.
@EminErtacPehlivan
@EminErtacPehlivan 3 ай бұрын
nice content , I wish you sell your ts course cheaper hence we could buy it my friend 😂
@enlightenedarchitect1673
@enlightenedarchitect1673 Ай бұрын
i wish you were a private tutor!
@superuser8636
@superuser8636 4 ай бұрын
Coming from C++, I love everything about TypeScript
@benjaoliva3383
@benjaoliva3383 4 ай бұрын
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 4 ай бұрын
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 4 ай бұрын
@@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 4 ай бұрын
Yes, & is the operator for union (this AND that), and | is the operator for intersection (either-or)
@joe-skeen
@joe-skeen 4 ай бұрын
Or just use extends:
@AkioJunichiro
@AkioJunichiro 4 ай бұрын
Another generics use is to work with interface, then you can pass any type that respect at minimum the interface structure
@themightysapien
@themightysapien 4 ай бұрын
You are a G.
@jasonlough6640
@jasonlough6640 4 ай бұрын
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 4 ай бұрын
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_ 3 ай бұрын
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 }
@dawid_dahl
@dawid_dahl 4 ай бұрын
With (string | number)[] you said, when the type returned from getFirstElement was “string | number”: “But TypeScript is not smart enough to know that (what we passed in was actually a string).” It’s not that TS is not smart enough. It was that the type was explicitly set as “string | number”, and since the function has not actually been run yet, there is no way TS could possibly know that, no matter how smart it was. It was simply respecting the programmer’s wishes, not being stupid. 😄
@tomasburian6550
@tomasburian6550 3 ай бұрын
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.
@HungNguyen-vi4rr
@HungNguyen-vi4rr 4 ай бұрын
8:00 no no, it is smart enough to force you to put the Generic Type there. If it automatically generated / inferred types there, Generic Type would be useless there: no validation errors, it works as 'any' type all the way :)). They need Generic Type to acknowledge that what type you want to validate "data"
@eyobsamuel8722
@eyobsamuel8722 4 ай бұрын
did you know? the intro music in the video is done by Kyle, with that guitar. @Kyle, please confirm.
@mygelbergstresser7729
@mygelbergstresser7729 4 ай бұрын
Could you cover a video about importing a JSON file and how to type that?
@stevezelaznik5872
@stevezelaznik5872 4 ай бұрын
type JsonPrimitive = null | string | number | boolean; type JsonData = JsonPrimitive | JsonData[] | { [key: string]: JsonData };
@Shyam_Mahanta
@Shyam_Mahanta 4 ай бұрын
Why you think extends is not that needed? How would build a higer order component in react using dynamic prop type ???
@ilyaskarim1952
@ilyaskarim1952 4 ай бұрын
I lost an interview because I didn't name the generics. I used generics for a long time. Interviewer asked me to explain generics but I was unable to explain because I didn't at that time that these are called generics 😅
@boxboxerson991
@boxboxerson991 4 ай бұрын
see this is good because it's not some vague opinion piece on how types are better than interfaces or some nonsense. exclusively stuff like this pls.
@jasonlough6640
@jasonlough6640 4 ай бұрын
Its weird, because one syntax function foo(n: string) says "this function will accept this kind of thing, which makes sure you only pass this kind of thing" while the other function foo(n:T) says "this function takes anything, but that anything is now consistent". The second one wont stop you from sending "apples" to function checkBankAccountBalance(n:T), so it seems counterproductive.
@stevezelaznik5872
@stevezelaznik5872 4 ай бұрын
The point of a generic is to define a relation between two or more things. For example a return type T of a function being the same as its argument type T. In your example the generic only applies to one thing, so it doesn’t define a relationship between anything.
@jasonlough6640
@jasonlough6640 4 ай бұрын
@@stevezelaznik5872 makes sense.
@albiummid
@albiummid 4 ай бұрын
Now generics are simplified 🎉
@tjohns92109
@tjohns92109 Ай бұрын
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 3 күн бұрын
JSDocs is a good alternative if you feel that way
@lambgoat2421
@lambgoat2421 3 ай бұрын
Thanks for these tips Spider-Man
Learn Web Components In 25 Minutes
24:21
Web Dev Simplified
Рет қаралды 183 М.
TypeScript Tutorial for Beginners
1:04:28
Programming with Mosh
Рет қаралды 1,1 МЛН
Сын Расстроился Из-за Новой Стрижки Папы 😂
00:21
Глеб Рандалайнен
Рет қаралды 3,1 МЛН
I Trapped Myself in a Box with Colored Smoke!
00:50
A4
Рет қаралды 18 МЛН
ONE MORE SUBSCRIBER FOR 4 MILLION!
00:28
Horror Skunx
Рет қаралды 56 МЛН
Ages 1 - 100 Decide Who Wins $250,000
40:02
MrBeast
Рет қаралды 123 МЛН
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 157 М.
10 Tailwind Classes I Wish I Knew Earlier
13:31
Web Dev Simplified
Рет қаралды 146 М.
How To Actually Get Hired In 2024
10:43
Web Dev Simplified
Рет қаралды 183 М.
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 274 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 154 М.
You might not need useEffect() ...
21:45
Academind
Рет қаралды 122 М.
TypeScript in React - COMPLETE Tutorial (Crash Course)
53:21
ByteGrad
Рет қаралды 172 М.
Top 6 React Hook Mistakes Beginners Make
21:18
Web Dev Simplified
Рет қаралды 554 М.
Why use Type and not Interface in TypeScript
14:12
ByteGrad
Рет қаралды 183 М.
Сын Расстроился Из-за Новой Стрижки Папы 😂
00:21
Глеб Рандалайнен
Рет қаралды 3,1 МЛН