Write a generic type Remove<T, key> which (a) removes all occurences of "key: PrimitiveType" e.g. "key: number;" in T, and (b) if T has "key: U" at some level, where U is not a primitive type, then it is converted into "U", and "key" is removed.
For example if I have the following type:
type Before = {
A: string
B: {
C: string
D: {
E: number
}
}
}
and I want to change it to this by, for example, Remove<Before, "D">
:
type After = {
A: string
B: {
C: string
E: number
}
}
Note that D
is removed but E
is remained
Other cases worth mentioning thanks to so_close
Case #1 Remove<T,”data”>
type T = {
data: {
data: string;
}
};
// would be
type T = { };
Case #2 Remove<T,”b”>
type T2 = {
a: {
b: string;
};
b: number;
}
// would be
type T2 = {
a: { };
}