Is there a way to specify a partial type in TypeScript that also makes all child objects partials as well? For example:
If you're looking for a quick and easy solution, check out the type-fest package, which has many useful prebuilt TypeScript types including the PartialDeep
type.
For a more technical and customizable solution, see this answer.
You can simply create a new type, say, DeepPartial
, which basically references itself:
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
Then, you can use it as such:
const foobar: DeepPartial<Foobar> = {
foo: 1,
bar: { baz: true }
};
See proof-of-concept example on TypeScript Playground.