MASTERING Type Manipulation in TypeScript Using RECURSION - Advanced TypeScript made EASY

preview_player
Показать описание
In the second video of our "Recursive Types" series we will figure out how we can replace certain types in existing types by using recursion to be able to even replace deeply nested property types.
Рекомендации по теме
Комментарии
Автор

A piece of excellent typing! I still don't understand why TypeScript doesn't provide itself a standard library with such helpful helper types....

coder_one
Автор

Hi
Why we dont need to check if is an object to do the recursion?
Reading feels like the recursion will never stop.

DA
Автор

thanks. is it possible to straight up omit the value altogether? I know Omit exists, but I mean omitting by property type, eg. derive a type from T but without any T properties whose values are of type `Date`

zandernoriega
Автор

Why isnt this causing any infinite recursion loop, when you have a property of type number or something that doesnt have Date as type or as supertype?

type DateReplacer<T> = {
[Key in keyof T]: Date extends T[Key] ? string : DateReplacer<T[Key]>
};

1. It iterates over each key of the passed Type T ([Key in keyof T])
2. It checks if the type of the key has Date as supertype (Date extends T[Key]):
a) Good case (Date extends Date): If Date is assignable to T[Key], the property type is replaced with string.
b) Recursion case: If not, it recursively applies DateReplacer to T[Key] => infinite recursion here?

FriendlyAce
Автор

Okay, I'm the third one who didn't understand "the magic of typescript" for it not looping into infinite recursion. I think it requires some clarification from author.
I generally not comprehend what is happening when non-Object gets into Replacer as the current T value. Will it return the T back without any changes? Why is that if so?

TheSome