Let‘s Fix Unmaintainable Types: Automatically Transform Legacy Types in TypeScript with Ease

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024
  • НаукаНаука

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

  • @malvoliosf
    @malvoliosf 2 месяца назад +49

    Hahaha, that’s nuts. Somebody told me that Typescript typing is itself Turing-complete but I never thought about the consequences.

  • @ouwyukha
    @ouwyukha Месяц назад +30

    I have to cross my finger and pray i won't ever use this on my entire life, what a beast 😂

  • @asdfasdfuhf
    @asdfasdfuhf Месяц назад +4

    I love these more advanced typescript videoes, you are my new favorite typescript youtuber. Keep them videos coming!
    Particularly, I am interested in seeing advanced usage of generics.

  • @dandogamer
    @dandogamer Месяц назад +3

    This is insane, i feel like if i used this i would hate myself for it 3 weeks later 😂

  • @shubitoxX
    @shubitoxX Месяц назад +12

    This is nuts, just do something simple since you need this code anyways when to map at runtime
    function toNewType(from: Legacy) {
    return {
    // ...
    }
    }
    type NewType = ReturnType
    (or specify the return type if you don't want the inferred type)

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +4

      @@shubitoxX i absolutely see your point. The problem with this approach is, that you always have to update your resulting type manually and you do not know when the legacy type gets a new property because it will not show a compile time error. But of course only use the approach shown if it gives a benefit to your code. In my production code I always strive for simplicity instead of making it clever :)

    • @1879heikkisorsa
      @1879heikkisorsa Месяц назад +3

      It may seem unnecessary, but if you have bigger projects with hundreds of types and multiple developers it's almost necessary to have some automated guards for changes like type additions. And the cool thing about the proposed approach in this video: It's generic. So once you've set it up it will just work for all your types.

    • @aaronscherling4927
      @aaronscherling4927 Месяц назад

      @@aleksander5298 ​​⁠​​⁠no need to degrade or demean anyone here. We are having, what I hope to be, an intelligent and objective conversation about Typescript types.
      1. The point of the channel is to teach typescript type, not the implementation. Therefore he doesn’t show the actual function. I’m sure his implementation would satisfy the type.
      2. There is a fundamental difference in perspective and scale that @Typed-Rocks approach is better suited for. The idea behind your types driving development and cross-team collaboration.
      a. In @shubitoxX’s proposed solution above. If you simply did the ‘ReturnOf’ solution your implementation creates the types, which can be useful, but less so if you’re trying to maintain a contract. Anyone could change the implementation and alter the types.
      b. If you have multiple teams, some doing a legacy refactor and some still implementing new features in the legacy system (a business needs to keep the lights on). Then this approach would allow for contract synchronization between multiple teams/types.
      I personally haven’t encountered this level of collaboration so I typically opt for declaring a second type.
      Keep up the amazing content @Typed-Rocks
      Have a good day 😊

  • @quynhvo279
    @quynhvo279 Месяц назад

    So great. Please release more videos like this

  • @asatyammis
    @asatyammis Месяц назад

    Never knew Typescript could do that. This video is the conversion portal of your viewers to subscribers.

  • @VonCarlsson
    @VonCarlsson Месяц назад +5

    I'm torn on this. I definitively see the value of having the [new and old] types be "linked". And I don't think it's nearly as complicated as some of the other commenters make it out to be. What would bother me, however, are two hypothetical scenarios: 1) What if they just never update it again? Then having a generic kinda complicated solution just becomes a potential source of bugs. 2) What if they update it a lot? Will my initial assumptions and understanding of the problem hold? How much can it grow before getting out of hand? Do I have a way out if that were to happen?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      @@VonCarlsson 100% true, these things I‘m showing are maybe sometimes too complicated for simple problems. I just want to show that it is possible to do such things. I would also only choose this approach in certain specific cases.

    • @VonCarlsson
      @VonCarlsson Месяц назад

      @@Typed-Rocks It's fine as an example. I didn't mean to discourage the use of more sophisticated types. As I said, the new and old types being "linked" is genuinely valuable. Getting an error at compile time is exactly what we want and definitively worth having a bit of extra complexity for! I'm just wary of it getting out of hand, because I've been there myself on more than one occasion 😅.

  • @profennador
    @profennador Месяц назад

    That is amazing! One way to automate things. Imagine have hundred of legacy properties, it is time saver!

  • @LucasSilva-jd2bf
    @LucasSilva-jd2bf Месяц назад

    Great video, very well explained. I'm not a web dev, so I had no idea Typescript's typing system ran so deep.

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      Thank you, really appreciate it. Especially coming from a non web dev 🙏

  • @AlbertoWagner
    @AlbertoWagner Месяц назад +1

    But this doesn’t solve the problem of receiving the old api data and converting it to the new type. 😢

  • @valcron-1000
    @valcron-1000 Месяц назад +1

    Excellent video. Would it be possible to generalize the "SearchAndReplaceAll" so we can pass different mapping functions instead of just "SearchAndReplace"?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      Thank you 🙏 and regading your question: It should be possible but I would create a new type for that to keep it better readable.

  • @Zoe-Kxe
    @Zoe-Kxe Месяц назад +3

    Thanks for Video 👍

  • @andydataguy
    @andydataguy Месяц назад

    Your channel is awesome 🙌🏾

  • @darkerisbetter8699
    @darkerisbetter8699 Месяц назад

    Instantly subscribed.

  • @Mego4884
    @Mego4884 Месяц назад

    Great demontration of TS power and great explanation as well. But wouldn't be easier to just define a new type for new api since it will probably be developed independently?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      Absolutely possible. If the legacy api is still evolving than it might be good to have a generic approach but otherwise 100%

  • @shaked1233
    @shaked1233 Месяц назад

    This was new to me, subscribing!

  • @noxiifoxi
    @noxiifoxi Месяц назад +1

    wow that's cool, subscribed ❤

  • @vdvman1
    @vdvman1 Месяц назад

    Wait, why does `DeepReplace` work for the types that aren't objects? How come you don't need a check for it being an object or being anything else? Shouldn't the mapped type fail when it sees the `string` and `number` types?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +1

      @@vdvman1 Excellent point. This is some TypeScript magic. It will only loop over the object types using the mapped types

    • @vdvman1
      @vdvman1 Месяц назад

      @@Typed-Rocks oh so that's just a thing with how mapped types work? Interesting 🤔 thanks!

  • @rodnoycry
    @rodnoycry Месяц назад

    I think the solution itself is cool because it showcases typescript generic magic. But example with converting APIs brings bad feelings about usage of such complicated approach in such important place. I really love doing complex types with generics and nested logic, but I think in such cases all this magic will create more problems than it solves

  • @sansu1947
    @sansu1947 Месяц назад +3

    i would hate you so much if I worked with you, overcomplicating instead of making an effort of cleaning up things even if takes a bit more time initially, this just adds to shit complexity and shit decisions made when the "legacy" thing happened

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +4

      Dont‘t worry. In my daily life i strive to simplicity, so we would not have any problems for sure ;). Your idea with the cleanup would be my first approach but if the type definitions come from external sources which we cannot change, we often don‘t have the ability to do that.

  • @Shadow10011
    @Shadow10011 Месяц назад

    Interesting, but why did it decide that the resulting id should be a a string and not a number? The legacy had one of each. Does this approach give any way to control this?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +1

      It should create a union of string|number

    • @Shadow10011
      @Shadow10011 Месяц назад +1

      @@Typed-Rocks Ah thank you, looked at bit closer and saw that the auto complete suggested a number, but the model accepted a string (timestamp 12:48). Might just be auto complete that could be improved.

  • @MichaelMammoliti
    @MichaelMammoliti Месяц назад

    I wouldn't fix unmaintainable code with more unmaintainable code. But I guess that the options exist

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      Exactly. It always depends when to use it.

    • @MichaelMammoliti
      @MichaelMammoliti Месяц назад

      @@Typed-Rocks usually you write unit tests and e2e code to migrate code. I wouldn’t rely on type checking alone.

  • @YagoFernades
    @YagoFernades Месяц назад

    This is the type equivalent of a spork, it's kinda neat, you say that you're going to use it, but you wont. And if you do you'll regret every second on it!

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      I know exactly what you mean 😁

  • @carlosterrazas8913
    @carlosterrazas8913 Месяц назад

    what is your font in VS?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      I‘m using IntelliJ but there I use Jetbrains Mono NL

  • @abdushakoor0099
    @abdushakoor0099 Месяц назад +1

    As someone who has mostly worked with java, kotlin an dart, i wanna say, what sorcery is this?

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +1

      It's really cool. I also started back with Java 5 and it's crazy what is possible with this language.

  • @user-tx3pv1xl6g
    @user-tx3pv1xl6g Месяц назад +1

    ah yes TypeScript, purely functional language.

  • @ndukachukz8067
    @ndukachukz8067 Месяц назад

    I thought I knew typescript

  • @zul.overflow
    @zul.overflow Месяц назад

    wizard

  • @Akarox
    @Akarox Месяц назад +1

    oh god! this is fucked up haha

  • @wertig6925
    @wertig6925 2 месяца назад +1

    🤘🏻

  • @afuzzybearsyoutubechannel2812
    @afuzzybearsyoutubechannel2812 Месяц назад

    go grab a beer on me

  • @DisfigurmentOfUs
    @DisfigurmentOfUs Месяц назад

    Cool

  • @حاتمأمحمد-د6د
    @حاتمأمحمد-د6د 2 месяца назад +5

    What is this shi**** my friend, stop using typescript, this is madness

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад +4

      @@حاتمأمحمد-د6د stay tuned for more 😁

    • @myname2462
      @myname2462 Месяц назад

      😂

  • @hlubradio2318
    @hlubradio2318 Месяц назад +2

    Negative I'm Pythonic

  • @gilneyn.mathias1134
    @gilneyn.mathias1134 Месяц назад +5

    ts was a mistake 😭

  • @ajdinhusic2574
    @ajdinhusic2574 Месяц назад +2

    All this effort, just for a freaking Type that is completely useless in the browser 🤣

  • @joseluisfernandezmateo7246
    @joseluisfernandezmateo7246 Месяц назад

    I use TS in my job, I do types...but, that kind of types...sorry I'm not into that LOL

    • @Typed-Rocks
      @Typed-Rocks  Месяц назад

      Absolutely fair point. I will also do this almost never in real projects but it shows the power of typescript. With great power comes great responsibility 😜

  • @parlor3115
    @parlor3115 Месяц назад +1

    This is ridiculous, if you're stuck maintaining a crappy app, propose a rewrite asap, or get the hell out of there. No program should be this convoluted