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
  • НаукаНаука

Комментарии • 23

  • @whatplan4335
    @whatplan4335 Год назад +2

    Great video
    Classes are a really nice way to quickly define a type and constructor in one spot with not that much code

  • @LuKaSSthEBosS
    @LuKaSSthEBosS Год назад +1

    Nice explanation for private fields! Been working with typescript classes for some time, and it's been really enjoyable! Great video

  • @gosnooky
    @gosnooky Год назад +2

    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.

  • @MindlessTurtle
    @MindlessTurtle Год назад +5

    Why do I get a bad feeling when using assertions and guards? Feels like building on shaky ground.

    • @doremicocoparis9410
      @doremicocoparis9410 Год назад +1

      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)

    • @otek_
      @otek_ Год назад +1

      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?

    • @doremicocoparis9410
      @doremicocoparis9410 Год назад

      For this you need to use an other pattern, the exhaust never pattern for example

  • @dimitro.cardellini
    @dimitro.cardellini Год назад +2

    Great video, thanks.
    Could you please record something about alternative approaches: factories and more exotic type classes?

  • @valikonen
    @valikonen 11 месяцев назад

    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.

  • @PickleRiiiiiiick
    @PickleRiiiiiiick Год назад

    Love it. Thank Andrew.

  • @reverser41
    @reverser41 Год назад

    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

  • @gantoreno
    @gantoreno Год назад

    Awesome video!
    Btw, what font are you using? Looks neat!

    • @andrew-burgess
      @andrew-burgess  Год назад

      Thanks! It's Mono Lisa!

    • @Noam-Bahar
      @Noam-Bahar Год назад

      @@andrew-burgess that's the best name for a font I've heard so far

  • @hansschenker
    @hansschenker Год назад

    In Typescript every value is also a type. The type fortyTow = 42 has the value 42, {} is of type object , ...

    • @jenspalmqvist
      @jenspalmqvist Год назад

      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.

    • @andrew-burgess
      @andrew-burgess  Год назад

      Good reminder!

  • @0tickpulse173
    @0tickpulse173 Год назад +1

    Your vim reminds me of nvchad

    • @andrew-burgess
      @andrew-burgess  Год назад

      Ha! I tried nvchad for a few days just recently, but switched to kickstarter.nvim. No magic, just a good base config.

    • @0tickpulse173
      @0tickpulse173 Год назад +1

      @@andrew-burgess lol, im setting up kickstarter.nvim too currently

  • @artemkotelevych2523
    @artemkotelevych2523 Год назад +1

    js is a joke

  • @thedelanyo
    @thedelanyo Год назад

    p.getFullName.bind(p) ,,, I'll rather try avoiding `this` whenever possible 😂😂😂

  • @Luxcium
    @Luxcium Год назад +1

    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 😅😅😅😅