patterntypescriptMajor
TypeScript: deep partial?
Viewed 0 times
partialtypescriptdeep
Problem
Is there a way to specify a partial type in TypeScript that also makes all child objects partials as well? For example:
This throws the following error:
Is there any way to make child nodes partials as well?
interface Foobar {
foo: number;
bar: {
baz: boolean;
qux: string;
};
}
const foobar: Partial = {
foo: 1,
bar: { baz: true }
};
This throws the following error:
TS2741: Property 'qux' is missing in type '{ baz: true; }' but required in type '{ baz: boolean; qux: string; }'.Is there any way to make child nodes partials as well?
Solution
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
For a more technical and customizable solution, see this answer.
PartialDeep type.For a more technical and customizable solution, see this answer.
Context
Stack Overflow Q#61132262, score: 61
Revisions (0)
No revisions yet.