I am currently working with the following type definitions:
type Target = number | undefined;
type MyObject = {
some: string;
properties: string;
id: Target;
}
I am trying to find a generic solution to replace instances of Target
with number
, for example:
type MyObjectTransformed = TargetToNumber<MyObject>;
/**
* MyObjectTransformed is now:
*
* {
* some: string;
* properties: string;
* id: number;
* }
*/
It's straightforward when Target
is always in the id
field, but what if Target
could appear anywhere? I need it to work like this too:
TargetToNumber<{
some: string;
other: Target;
properties: string;
}>
And to make things more challenging, it should also handle nested occurrences. For instance, replacing Target
with number
in this scenario:
TargetToNumber<{
some: string;
properties: string;
nested: {
arbitrarily: {
deep: Target;
};
};
}>
If you're wondering why I have such an unusual request, you can find more information here.