![Cover image for 7 Secret TypeScript Tricks Pros Use ππ€«](https://media.dev.to/cdn-cgi/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktesxigi964m1u8lyqi0.png)
TypeScript is an outstanding tool to make our lives easier & avoiding bugs, yet sometimes feels overwhelming to use.
This article outlines 7...
For further actions, you may consider blocking this person and/or reporting abuse
Nice tips. In my opinion the main thing that separates pros from newbies is that pros turn on all the strict options and still manage to avoid
as
like the plague because they know a ton of tools like these to avoid it. Death toas
!It's now generally recommended to write index signatures using
Record
e.g.type ParticipantData = Record<string, ParticipantStatus>
.Great tips! I'd like to contribute one more suggestion. Instead of using "any" for typing inputs in your type guard, it's safer to use "unknown". This approach encourages you to first check if a parameter is of type object before accessing its properties. You can see this here: youtube.com/watch?v=p-YEN-9KnRo
This is a nice summary of TypeScript tricks. Thank you for putting it together.
Using TypeScript has changed my approach to programming for the Web. And I'm grateful to have access to this technology.
I was looking for this type of article. Where I can learn all about props types. Thanks for sharing
Hello. Do you mean React prop types? What prop types exactly?
Thank you for these tips. Especially the Partial, Pick, Omit & Required Types help me, as I only knew
Partial<>
, but I have a usecase for the other ones too.Very good article and examples! :)
Nice collections, thanks for listing all these...
Great
Wow, I didn't know that I had been a TS Pro all this time!
Thanks, this was very helpful.
Thanks, this was very helpful.
Really nice list!! Thank you!!
Very helpful tips.
Nice
Thank you for the great explanation. It helped me review the topic.
The
as const
thing is also not really correct. If you don't want to be able to mutate properties you should usereadonly
before each property.as const
is used to produce compile-time constants, for example to allow you to map names to string literals at compile time.Great article and excellent tips.
Some of them are very useful.
I've created a repo to test out the above tips & tricks on my local machine. It is public if anyone wants to clone the repo and tests the tricks that Tapajyoti Bose shared with us.
github.com/Yiannistaos/learning-ty...
These are wonderful for design time. But you should make it clear that this opens you for lots of runtime problems, and am those check need to them be fine elsewhere. All of these types are removed when the code is 'compiled'.
So many bugs introduced into codebases because this loss of type information at runtime is not understood.
All those checks you mentioned still need to be done somewhere, but not everywhere.
Enum is considered bad practice.
It's more flexible to use a const:
I think the second one is more about mapped value?
Π‘ongratulations π₯³! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up π