As template literal types are now supported in TypeScript, is it possible to have something like the following?
interface Data {
a: number;
b: {
c: number;
d: number;
}
}
type OmitDeep<T, K> = ...
type WithoutC = OmitDeep<Data, 'b.c'>
Where WithoutC will be inferred as:
interface WithoutC {
a: number;
b: {
d: number
}
}
You can use this type to map dotted path to an array path:
type UnDot<T extends string> =
T extends `${infer A}.${infer B}` ? [A, B] : ''
type ProperyArray = UnDot<'a.b'>; // ["a", "b"]
And then use this answer to remove nested properties:
Types for deleting object at specific nested path
Edit for any number of nesting levels:
type UnDot<T extends string> =
T extends `${infer A}.${infer B}`
? [A, ...UnDot<B>]
: [T];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With