Hahaha, that’s nuts. Somebody told me that Typescript typing is itself Turing-complete but I never thought about the consequences.
@ouwyukhaАй бұрын
I have to cross my finger and pray i won't ever use this on my entire life, what a beast 😂
@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Ай бұрын
So great. Please release more videos like this
@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Ай бұрын
@@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Ай бұрын
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Ай бұрын
@@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Ай бұрын
@@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Ай бұрын
@@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Ай бұрын
This is insane, i feel like if i used this i would hate myself for it 3 weeks later 😂
@asatyammisАй бұрын
Never knew Typescript could do that. This video is the conversion portal of your viewers to subscribers.
@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Ай бұрын
@@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Ай бұрын
@@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Ай бұрын
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-Rocks26 күн бұрын
I know exactly what you mean 😁
@profennadorАй бұрын
That is amazing! One way to automate things. Imagine have hundred of legacy properties, it is time saver!
@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Ай бұрын
Thank you, really appreciate it. Especially coming from a non web dev 🙏
@Zoe-KxeАй бұрын
Thanks for Video 👍
@AlbertoWagnerАй бұрын
But this doesn’t solve the problem of receiving the old api data and converting it to the new type. 😢
@darkerisbetter8699Ай бұрын
Instantly subscribed.
@shaked1233Ай бұрын
This was new to me, subscribing!
@andydataguyАй бұрын
Your channel is awesome 🙌🏾
@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Ай бұрын
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Ай бұрын
wow that's cool, subscribed ❤
@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Ай бұрын
Absolutely possible. If the legacy api is still evolving than it might be good to have a generic approach but otherwise 100%
@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Ай бұрын
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Ай бұрын
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Ай бұрын
As someone who has mostly worked with java, kotlin an dart, i wanna say, what sorcery is this?
@Typed-RocksАй бұрын
It's really cool. I also started back with Java 5 and it's crazy what is possible with this language.
@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Ай бұрын
@@vdvman1 Excellent point. This is some TypeScript magic. It will only loop over the object types using the mapped types
@vdvman1Ай бұрын
@@Typed-Rocks oh so that's just a thing with how mapped types work? Interesting 🤔 thanks!
@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Ай бұрын
It should create a union of string|number
@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.
@carlosterrazas891329 күн бұрын
what is your font in VS?
@Typed-Rocks29 күн бұрын
I‘m using IntelliJ but there I use Jetbrains Mono NL
@MichaelMammolitiАй бұрын
I wouldn't fix unmaintainable code with more unmaintainable code. But I guess that the options exist
@Typed-Rocks26 күн бұрын
Exactly. It always depends when to use it.
@MichaelMammoliti25 күн бұрын
@@Typed-Rocks usually you write unit tests and e2e code to migrate code. I wouldn’t rely on type checking alone.
@user-qc2yb7ki9yАй бұрын
What is this shi**** my friend, stop using typescript, this is madness
@Typed-RocksАй бұрын
@@user-qc2yb7ki9y stay tuned for more 😁
@myname2462Ай бұрын
😂
@user-tx3pv1xl6gАй бұрын
ah yes TypeScript, purely functional language.
@hlubradio2318Ай бұрын
Negative I'm Pythonic
@zul.overflowАй бұрын
wizard
@wertig6925Ай бұрын
🤘🏻
@ndukachukz8067Ай бұрын
I thought I knew typescript
@joseluisfernandezmateo7246Ай бұрын
I use TS in my job, I do types...but, that kind of types...sorry I'm not into that LOL
@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Ай бұрын
Cool
@AkaroxАй бұрын
oh god! this is fucked up haha
@ajdinhusic2574Ай бұрын
All this effort, just for a freaking Type that is completely useless in the browser 🤣
@gilneyn.mathias1134Ай бұрын
ts was a mistake 😭
@afuzzybearsyoutubechannel2812Ай бұрын
go grab a beer on me
@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Ай бұрын
Nice code, but in real life, you don't want to have magic functions, you will just hardcode your new types :)