No video

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

  Рет қаралды 12,125

Typed Rocks

Typed Rocks

Күн бұрын

Пікірлер: 66
@malvoliosf
@malvoliosf Ай бұрын
Hahaha, that’s nuts. Somebody told me that Typescript typing is itself Turing-complete but I never thought about the consequences.
@ouwyukha
@ouwyukha Ай бұрын
I have to cross my finger and pray i won't ever use this on my entire life, what a beast 😂
@asdfasdfuhf
@asdfasdfuhf Ай бұрын
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.
@quynhvo279
@quynhvo279 Ай бұрын
So great. Please release more videos like this
@shubitoxX
@shubitoxX Ай бұрын
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 Ай бұрын
@@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 Ай бұрын
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.
@aleksander5298
@aleksander5298 Ай бұрын
@@Typed-Rocks but the problem with your approach is, you just have types, and you don't have mapping function, so you write magic types and you don't validate in any way your data :) proposed solution is way better, but yeah, you are just youtuber and never was in real project with real problems :)
@aleksander5298
@aleksander5298 Ай бұрын
​@@1879heikkisorsa So you're saying you've never worked in enterprise solutions? No self-respecting developer would use a magic mapping function to change the schema in such a way, there are so many possibilities for bad mapping here that only an idiot would do it, or someone who thinks he is a smart developer, but in truth only creates confusion in the project, pathetic :)
@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 😊
@dandogamer
@dandogamer Ай бұрын
This is insane, i feel like if i used this i would hate myself for it 3 weeks later 😂
@asatyammis
@asatyammis Ай бұрын
Never knew Typescript could do that. This video is the conversion portal of your viewers to subscribers.
@VonCarlsson
@VonCarlsson Ай бұрын
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 😅.
@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 26 күн бұрын
I know exactly what you mean 😁
@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 🙏
@Zoe-Kxe
@Zoe-Kxe Ай бұрын
Thanks for Video 👍
@AlbertoWagner
@AlbertoWagner Ай бұрын
But this doesn’t solve the problem of receiving the old api data and converting it to the new type. 😢
@darkerisbetter8699
@darkerisbetter8699 Ай бұрын
Instantly subscribed.
@shaked1233
@shaked1233 Ай бұрын
This was new to me, subscribing!
@andydataguy
@andydataguy Ай бұрын
Your channel is awesome 🙌🏾
@sansu1947
@sansu1947 Ай бұрын
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 Ай бұрын
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.
@noxiifoxi
@noxiifoxi Ай бұрын
wow that's cool, 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%
@valcron-1000
@valcron-1000 Ай бұрын
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.
@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
@abdushakoor0099
@abdushakoor0099 Ай бұрын
As someone who has mostly worked with java, kotlin an dart, i wanna say, what sorcery is this?
@Typed-Rocks
@Typed-Rocks Ай бұрын
It's really cool. I also started back with Java 5 and it's crazy what is possible with this language.
@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 Ай бұрын
@@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!
@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 Ай бұрын
It should create a union of string|number
@Shadow10011
@Shadow10011 Ай бұрын
@@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.
@carlosterrazas8913
@carlosterrazas8913 29 күн бұрын
what is your font in VS?
@Typed-Rocks
@Typed-Rocks 29 күн бұрын
I‘m using IntelliJ but there I use Jetbrains Mono NL
@MichaelMammoliti
@MichaelMammoliti Ай бұрын
I wouldn't fix unmaintainable code with more unmaintainable code. But I guess that the options exist
@Typed-Rocks
@Typed-Rocks 26 күн бұрын
Exactly. It always depends when to use it.
@MichaelMammoliti
@MichaelMammoliti 25 күн бұрын
@@Typed-Rocks usually you write unit tests and e2e code to migrate code. I wouldn’t rely on type checking alone.
@user-qc2yb7ki9y
@user-qc2yb7ki9y Ай бұрын
What is this shi**** my friend, stop using typescript, this is madness
@Typed-Rocks
@Typed-Rocks Ай бұрын
@@user-qc2yb7ki9y stay tuned for more 😁
@myname2462
@myname2462 Ай бұрын
😂
@user-tx3pv1xl6g
@user-tx3pv1xl6g Ай бұрын
ah yes TypeScript, purely functional language.
@hlubradio2318
@hlubradio2318 Ай бұрын
Negative I'm Pythonic
@zul.overflow
@zul.overflow Ай бұрын
wizard
@wertig6925
@wertig6925 Ай бұрын
🤘🏻
@ndukachukz8067
@ndukachukz8067 Ай бұрын
I thought I knew typescript
@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 😜
@DisfigurmentOfUs
@DisfigurmentOfUs Ай бұрын
Cool
@Akarox
@Akarox Ай бұрын
oh god! this is fucked up haha
@ajdinhusic2574
@ajdinhusic2574 Ай бұрын
All this effort, just for a freaking Type that is completely useless in the browser 🤣
@gilneyn.mathias1134
@gilneyn.mathias1134 Ай бұрын
ts was a mistake 😭
@afuzzybearsyoutubechannel2812
@afuzzybearsyoutubechannel2812 Ай бұрын
go grab a beer on me
@parlor3115
@parlor3115 Ай бұрын
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
@aleksander5298
@aleksander5298 Ай бұрын
Nice code, but in real life, you don't want to have magic functions, you will just hardcode your new types :)
7 Awesome TypeScript Types You Should Know
8:57
Josh tried coding
Рет қаралды 83 М.
Please Help Barry Choose His Real Son
00:23
Garri Creative
Рет қаралды 24 МЛН
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН
Linus Torvalds: Speaks on Hype and the Future of AI
9:02
SavvyNik
Рет қаралды 188 М.
FIRED For Using React?? | Prime Reacts
33:16
ThePrimeTime
Рет қаралды 423 М.
This Is Why Managers Don't Trust Programmers...
28:04
Thriving Technologist
Рет қаралды 251 М.
When RESTful architecture isn't enough...
21:02
Dreams of Code
Рет қаралды 274 М.
TypeScript's Next Release Is Wild
18:36
Theo - t3․gg
Рет қаралды 68 М.
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
12:35
NeetCode's Hot Take Is SO Good
35:48
ThePrimeTime
Рет қаралды 357 М.
Generics in Typescript
12:27
Hitesh Choudhary
Рет қаралды 19 М.
Why is everyone LYING?
7:56
NeetCodeIO
Рет қаралды 274 М.
Please Help Barry Choose His Real Son
00:23
Garri Creative
Рет қаралды 24 МЛН