HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptMajor

TypeScript: deep partial?

Submitted by: @import:stackoverflow-api··
0
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:

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 PartialDeep type.

For a more technical and customizable solution, see this answer.

Context

Stack Overflow Q#61132262, score: 61

Revisions (0)

No revisions yet.