TypeScript Wizardry: Recursive Template Literals

TypeScript Wizardry: Recursive Template Literals

Tech Talks with Simon

1 год назад

37,045 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

enjay86
enjay86 - 15.05.2023 01:14

au, my brain!

Ответить
Boštjan Pišler
Boštjan Pišler - 09.05.2023 22:19

Wow, that's crazy :D didn't know that was possible!

Ответить
Confused Horse
Confused Horse - 05.05.2023 16:04

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"

Ответить
Karlsson
Karlsson - 04.05.2023 23:22

Legendary

Ответить
Trọng Vĩnh Nguyễn
Trọng Vĩnh Nguyễn - 04.05.2023 18:10

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

Ответить
Eric Wingren
Eric Wingren - 04.05.2023 10:03

Great video!

My version for this case would be
type PathTo<T extends Record<string, unknown>> = keyof {

[K in keyof T as T[K] extends Record<string, unknown>
? `${K & string}.${PathTo<T[K]> & string}`
: K & string]: any;
};

Ответить
Daniel Lynch
Daniel Lynch - 04.05.2023 09:13

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.

Ответить
Alexey Nikolaev
Alexey Nikolaev - 03.05.2023 07:16

very educational video 👍 would be great to see more videos about complex types in TS and you find a solution for them

Ответить
Helio Alves
Helio Alves - 28.04.2023 15:29

Wow great stuff. You've just gained a new subscriber :).
I've been getting more and more into Typescript and I often struggle with recursion. I think the "variables" name in types aren't always the best :) things like K and T aren't very descriptive and it makes it really hard to understand what is going on :). I think one nice thing would be to comment the most challenging parts so you know what is going on.
But I loved your process though, thank you very much the great video.

Ответить
Adam Griffin
Adam Griffin - 28.04.2023 04:19

Man, that's rough! LOL It's an elegant solution but it's not very legible. It seems like once we get to the level of complexity where we need to invoke the term "type-level programming" that Typescript's terse syntax doesn't always let you express things in a way that's suitable for maintainability. You see the same kind of shortcomings with complex regular expressions. It starts to feel like you would almost want an extended syntax to deal with that sort of thing. Some languages actually do have alternative ways to build regexes that are more verbose but easier to read. Maybe MS will do something similar for Typescript in the future.

Ответить
Володимир Савчук
Володимир Савчук - 27.04.2023 22:12

_.get()

Ответить
Thomas A. Anderson
Thomas A. Anderson - 27.04.2023 01:12

Awesome 👌👏👏👏👏👏👏👏👏

Ответить
shaikh danish
shaikh danish - 25.04.2023 22:42

Can we also program the object?

Ответить
Alex Trofimov
Alex Trofimov - 24.04.2023 23:24

Why do you need to do ": keyof. { [K in EXP]: any } "instead of ": EXP"?

Ответить
Coffee and bytes
Coffee and bytes - 24.04.2023 14:35

This is my issue with TypeScript tutorials, there are only two skill levels being taught: Absolute Beginner or Dark Wizard.

Ответить
Jonny Freeman
Jonny Freeman - 22.04.2023 15:27

Man, very good, thanks for sharing! With the TS one actually have a huge boilerplate even before actually start coding - but it is definitely worth of that!

Ответить
nazaka
nazaka - 21.04.2023 19:29

i see it right on the next day after doing exactly the same :)

Ответить
r
r - 19.04.2023 10:31

been playing around with something similar, ended up with


type DotNotation<T> = {
[K in keyof T]: T[K] extends object
? `${K & string}.${DotNotation<T[K]>}`
: K & string;
}[keyof T];


type Foo = DotNotation<typeof my_obj>

Ответить
Max T
Max T - 18.04.2023 11:59

How would you also allow "hello.greetings" for instance ? (stop the type mid-path)

It can sometimes be useful, although this is not the case in your example obviously.

Anyways, great explanation, thank you for that !

Ответить
A I
A I - 17.04.2023 19:32

Imagine geting hired somewhere and your first task is to debug this while Simon is no longer working there

Ответить
ALONUCLEAR
ALONUCLEAR - 17.04.2023 12:16

This was a very interesting video. I really enjoyed it

Ответить
JetBoom
JetBoom - 17.04.2023 03:36

TS was a mistake

Ответить
Bobby Bundlez
Bobby Bundlez - 17.04.2023 02:43

so lame lol. TS at this level takes the joy and fun out of literally everything

Ответить
avneet12284
avneet12284 - 17.04.2023 02:19

No repository link? Not good.

But excellent video

Ответить
A Rogue Otaku
A Rogue Otaku - 16.04.2023 16:03

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.

Ответить
Showny Kim
Showny Kim - 16.04.2023 14:42

"T[K]" I'm sure people will try to backtrack a lot to understand this. It's still a pain in the ass 🤣🤣

Ответить
Karl Kristian Lockert
Karl Kristian Lockert - 16.04.2023 12:53

Could perhaps ‘infer K’ be used instead of ‘& string’ to Get the types correctly and also provide some fallback?

Ответить
Владислав Портной
Владислав Портной - 15.04.2023 13:11

Great video! Thanks!

Ответить
Dmitriy Nesterkin
Dmitriy Nesterkin - 15.04.2023 06:28

This is a great example for why not to use Typescript. What exactly does the baby-sitting code accomplish? Imagine the amounts of time wasted writing, reading, and maintaining shit like this. Write plain JS and use tests to assert code quality.

Ответить
Mark Emerson
Mark Emerson - 14.04.2023 17:05

another reason i detest typescript...

Ответить
Веданта-крит дас
Веданта-крит дас - 14.04.2023 15:58

Thanks for the video. Trying do something lake this without using recursive type… this solution cooler!

Ответить
Sune Scheuerlein-Tougaard
Sune Scheuerlein-Tougaard - 14.04.2023 13:44

That is a very nice explanation. Would it be possible to make the t function more strict on the return type? so that instead of just returning a string it returns a string litteral? so that when you add the key the complier already knows what the sting is and shows that on hover.

Ответить
Yuuki DeName
Yuuki DeName - 14.04.2023 06:52

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.

Ответить
tosun abi
tosun abi - 14.04.2023 02:14

Piece of art for a new TS user.

Ответить
WorstmetJus91
WorstmetJus91 - 14.04.2023 01:19

Very cool! React-hook-form I believe uses exactly this syntax to access form data but i do not know how they made the typing work. Perhaps worth the look? I know I will now!

Ответить
Paweł Graczyk
Paweł Graczyk - 13.04.2023 22:37

Useless

Ответить
Māris Samats
Māris Samats - 13.04.2023 09:42

Didn't know you could put string literals to generate keys! I was always wondering, how react solved "data-xyz" props. I was looking into its types, but couldn't find it there.. now I know it is some version of this!

Ответить
Cottidae
Cottidae - 12.04.2023 23:55

This is honestly really cool. I just wish TypeScript wasn't JavaScript, because in the end you can just throw anything into whatever TypeScript function is created and watch it break. I know many who struggle with TypeScript due to IDE performance as well, which is unfortunate. More of an IDE problem though (and likely relying too much on inference).

Ответить
simpel
simpel - 12.04.2023 22:57

That was awesome!

Ответить
shadowsir
shadowsir - 12.04.2023 22:13

Whoa, didn't know this was possible, great job explaining it! :D

Ответить
Vali Dr
Vali Dr - 12.04.2023 19:26

Would be much easier to use a recursive function to generate those strings. If you add an extra level, you need to change all your code...

Ответить
Jamie Lievesley
Jamie Lievesley - 12.04.2023 17:23

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

Ответить
Raymond Michael
Raymond Michael - 12.04.2023 15:48

Thanks buddy 😮, this got me;
Still getting my feet wet with Typescript 😊

Ответить
Lewis Cowles
Lewis Cowles - 12.04.2023 09:26

Do you have any stats on what these "fancy types" do to build times?

Ответить
Kyle Clark
Kyle Clark - 12.04.2023 07:11

This is fantastic. Keep it up!

Ответить
Jesse Baker
Jesse Baker - 12.04.2023 00:27

Great video, localws are always a pain and I've had this issue in the past. Very elegant solution

Ответить
Joppe Koers
Joppe Koers - 11.04.2023 23:46

This was great!
But please share the code so I dont have to image-to-text it.

Ответить