5 Tips for Classier TypeScript
HTML-код
- Опубликовано: 9 июл 2024
- If you're using classes a lot in your TypeScript code, here are several useful tips for you!
5 Tips
00:05 - Classes are both types and values
02:18 - Truly Private Fields
03:41 - Parameter Properties
04:36 - Assertions & Guards
07:07 - Handling the type of `this`
My Links
shaky.sh
shaky.sh/tools
/ andrew8088
mastodon.social/@andrew8088
#typescript #coding #programming #javascript #ecmascript #classes #oop - Наука
Great video
Classes are a really nice way to quickly define a type and constructor in one spot with not that much code
Nice explanation for private fields! Been working with typescript classes for some time, and it's been really enjoyable! Great video
Interesting. I've used the 'this' argument to create sort of an active record to classes that are serializable and validatable with class-transformer and class-validator, so I can call something like User.from({ id: 1, name: 'Bob' }) to pass a POJO that was JSON into an instance of the class. I never really understood how it works, but it does.
Why do I get a bad feeling when using assertions and guards? Feels like building on shaky ground.
Actually works well, you are just making explicit your invariants. Doesn't solve bad designs or bugs for you, but when they happens you locate them MUCH MORE quickly (which gives you an opportunity to iterate on your design)
I know what you mean. The assertion predicate feels like it *could* be quite arbitrary and prone to going out of sync. For instance, if the type gets an additional field that would have to be included in your assertion's type predicate for it to remain valid, how would you know?
For this you need to use an other pattern, the exhaust never pattern for example
Great video, thanks.
Could you please record something about alternative approaches: factories and more exotic type classes?
Very nice list. Also soft private fields can be accessed with: component['my_db_pass']. In angular we use a lot this approach in tests.
It will be great to make a video for adding an external npm package and his types.
Love it. Thank Andrew.
regarding your last tip, is there any way to make every instance method of a class to be called with instance, how can i make it throw or make invalid when getFullname() is called
Awesome video!
Btw, what font are you using? Looks neat!
Thanks! It's Mono Lisa!
@@andrew-burgess that's the best name for a font I've heard so far
In Typescript every value is also a type. The type fortyTow = 42 has the value 42, {} is of type object , ...
But if you do 'let something: {}' you can assign everything except null and undefined to that variable, so be careful using that if you want the thing to be an actual object.
Good reminder!
Your vim reminds me of nvchad
Ha! I tried nvchad for a few days just recently, but switched to kickstarter.nvim. No magic, just a good base config.
@@andrew-burgess lol, im setting up kickstarter.nvim too currently
js is a joke
p.getFullName.bind(p) ,,, I'll rather try avoiding `this` whenever possible 😂😂😂
Yea Classes are _way better than all that hype about Functional programming…_ I love using classes to create my *Functors,* extend them into *Monad,* but it is cool also with a *Nothing* to create some *Maybe* and then also creating some *Left* and *Right* and *Either* the usual stuff… _Andrew_ 🦸🏻♂️ has a pretty interesting video about an *Optional* you must absolutely look into it, the homework 📚 to the reader is to use a class to implement the Option type of his other video 😅😅😅😅