When working with types in TypeScript, we assume that we know what kind of type we will be working with.
For instance to define this log function:...
For further actions, you may consider blocking this person and/or reporting abuse
One interesting feature missing in that description is that you can limit what types a generic type can be using
extends
; multiple types can be expressed via union.Definitely a big pro, I think in tomorrow's article I actually use multiple types 🙌.
Generics in general are such a blessing and great way to control types.
Java devs will find this feature very familiar :)
In C# the type passed in may be inferred. It that true here?
Generics can be inferred indeed.
If you define them like this for instance, in which case you can pass the inferred type you want.
But in general it will default to the T anyhow. this could just prove to be a security fallback.
This is help me to understand
Glad it helped Nazmul 💖
Indeed , Typescript generics are life saving.
This post was really helpful 👍
Thanks MirAli!
Oh nice! I'm just learning Typescript and I had no idea of generics. I would have defaulted to using "any".
Thanks for the tip!
Glad it helped!
Generics are so powerful and indeed a valid replacement of any in most cases (when used correctly).
They have way more power than described here, so they can solve a lot of issues 🤯
Awesome!
They definitely take some getting used to, and practive.