I have a nested object of optional properties. I found a way to declare this type via this answer. Now I'm trying to figure out how to apply this type to my object interfaces without having to modify every object that uses the interface. Some examples to make this clearer.
Here is a minimal reproducible example.
Here is the recursive object type:
export type PartialRecursive<T> = T extends object
? {
[K in keyof T]?: PartialRecursive<T[K]>;
}
: T;
Sample interface:
export interface Foo {
bar: boolean;
bax: boolean;
}
Simple object (NOTE: the array is required here):
import { Foo } from './types';
const foo: Foo[] = [
{
bar: true
}
]
To use the new interface I could do this:
import { Foo, PartialRecursive } from './types';
const foo: PartialRecursive<Foo>[] = [{...}];`
But I would prefer to apply this PartialRecursive
type to the interface where it is declared, but I'm not sure how to do it. I have a lot of objects and would prefer to implement this type
against the interface instead of each implementation that uses it.
I hope this is clear, checked the TS docs, but it was hard to find what I was looking for.