TypeScript Wizardry: Recursive Template Literals

preview_player
Показать описание
In this one, I'll show you some template literal magic for accessing deeply nested objects in TypeScript. If you like pushing the boundaries of type-level programming, this video is for you!
Рекомендации по теме
Комментарии
Автор

I use types all the time (many languages) but I was not even aware "type-level programming" is a thing :)
TypeScript is at another level.

YilmazDurmaz
Автор

I was casually watching this in the living room and was really excited because it's just extraordinarily good content. But as soon as I was done I turned around to see my girl friend judging me. She called me a nerd and now I am happy in two ways. :>

Thank you for sharing this and "yolo"

confused_horse
Автор

Great content man, I think this type of advanced ts videos are really valuable, and it's not something you find often (at least with this quality). Keep the great job.

brigadafitness
Автор

Damn dude. That big brain move of intersecting string with the T[K] blew my mind. This video is going straight to my favorites playlist. You got a sub from me.

arogueotaku
Автор

Incidentally I needed something like this for work a few weeks ago. Found some stuff on Stack overflow, but this is the easiest solution to this problem I've found. Thank you.

Only way this video could be better is if you addressed how to have multiple "stop" value types, string in this example, like string | number | string[], but then infer each type from the multiple addressed to right path key. Hopefully that made sense. I don't even know how possible that is but I'm curious to know.

It's rare that Youtube recommends video this well. Subscribed. Great video!

gnarusg
Автор

This is amazing black magic and I’m glad I watched the video.

If I ever saw anyone submit anything remotely similar to this in a PR it would get a Deny so fast my left click would break the sound barrier.

daniellynch
Автор

This is definitely my jam. Have stumbled upon many of these, what appeared to me to be roadblocks, glad to see they are traversable.

SecularDarwinism
Автор

I actually used this in our company to name the fields in the api responses as we get Keys that are in objects or arrays, and we use them parsed in a specific way (with __ instead of .) and It was so difficult for me to manage to do this. I think this is greatly explained and would've been so Happy to find this video when I did this 😂

Athetss
Автор

I know this is mainly a demonstration of advanced TS features, and it also helps when having to write type declarations for existing libraries. And sometimes it's the kind of API you want to have.

But for new code, personally I would think about keeping it simple and just use e.g. t().greetings.morning instead of t("greetings morning"), where t = l => locales[l] (which is even one character shorter).

This way no recursion is required and you get the same autocomplete suggestions. And you'll also get the exact type at given key for free, which somebody in the comments suggested could be used parameters etc.

yuukidename
Автор

Great video!
One thing I would've done differently(and i'd love to hear your opinion) is- instead of manipulating the key of the mapped type, I would do the key-mapping-shenanigans you did in the values of the mapped type, then indexed using [keyof T]. it would probably be cleaner, using both sides of the object, and i also think you wouldn't need the second intersection at 11:50 since the return type wouldn't be a PropertyKey, rather a string(as the values would only be strings)
its a slight knitpicking, would love to hear your opinion
still, loved seeing you going through the problem, looking forward to more content!

dolevgo
Автор

This video is Amazing ! I needed a lite translation hook for my project and didn't want to fiddle with libraries for it. This is just perfect! I adapted it to be able to loop trough several files of translate keys and it works wonderfully !

Thank you for that!

maximemondello
Автор

Fantastic! Just in time for me, but I also love the way you reason about it.

aram
Автор

Cool video Simon. I learned a lot just in the first 5 min. Had to rewind a few times in there hehe. Audio sounds great too!

webbae
Автор

Ah, excellent! When I came upon this problem years ago, this was not possible yet in Typescript. I had to solve the problem in a more janky way. But this is so simple and sensible now, I can rewrite it nicely, thank you

Jamiered
Автор

I did something similar for another project, I made K extends string the outermost ternary operation so that in the true branch I didn’t need to do K & string, as in that branch K is narrowed to some string type. I used this type (I called it dot path) to map keys of one type to keys of another type based on a suffix in each key. Type level programming is my favorite feature of TypeScript (also probably the only feature I like tbh) because it feels like doing a proof, and also because the literal types make for a really good developer experience since the LSP can suggest strings from that type.

rahul
Автор

Wow I am truly amazed by this video. I considered myself as a mid advanced typescript user, but I just never got my brain that far... hoping to see much more typescript from you. Great work there

paulborek
Автор

I agree with most comments, I would like to see more TS content, very interesting and useful!

bamboo
Автор

these types might be very useful when creating sql queries, using nested relations, with an ORM such as typeorm. Very interesting thks

jeromealtariba
Автор

I have though about this many times and though it was impossible in TS.
Now you've clear my mind.
Thank you!

trongvinhnguyen
Автор

great explanation! I didn't know about that `K & string` trick, very useful :) thanks

tezismith