So here is what I came up with to create a recursive DeepRequired
type.
Input
Two generic type parameters:
T
for the base type Example
P
for an union type of tuples, that represent our "required object property paths" ["a", "b", "c"] | ["one", "two", "three"]
(similar to lodash object paths via get)
Example flow
- Grab all required properties in top level
P[0]
: "a" | "one"
- Create intersection type/concatenation of required and non required object properties
We include all properties from Example
and additionally create a mapped type to remove ?
and the undefined
value for each optional property that is to be changed to required. We can do that by using the built-in types Required
and NonNullable
.
type DeepRequired<T, P extends string[]> = T extends object
? (Omit<T, Extract<keyof T, P[0]>> &
Required<
{
[K in Extract<keyof T, P[0]>]: NonNullable<...> // more shortly
}
>)
: T;
- The type must be somehow recursive for sub properties. That implies, we also have to find a way to "shift" types from the tuple
T
to iteratively get the next required sub property in the path. To do that, we create a helper tuple type Shift
(more on the implementation shortly).
type T = Shift<["a", "b", "c"]>
= ["b", "c"]
- Challenging thing is, we want to pass in an union of tuples (aka many required paths), not just one. We can make use of distributive conditional types for this and use another helper
ShiftUnion
capable to distribute unions of tuples over the conditional type containing Shift
:
type T = ShiftUnion<["a", "b", "c"] | ["one", "two", "three"]>
= ["b", "c"] | ["two", "three"]
- We then can get all required properties for the next sub paths by simply selecting the first index:
type T = ShiftUnion<["a", "b", "c"] | ["one", "two", "three"]>[0]
= "b" | "two"
Implementation
Main type DeepRequired
type DeepRequired<T, P extends string[]> = T extends object
? (Omit<T, Extract<keyof T, P[0]>> &
Required<
{
[K in Extract<keyof T, P[0]>]: NonNullable<
DeepRequired<T[K], ShiftUnion<P>>
>
}
>)
: T;
Tuple helper types Shift
/ShiftUnion
We can infer the tuple type, that is shifted by one element, with help of generic rest parameters in function types and type inference in conditional types.
// Analogues to array.prototype.shift
export type Shift<T extends any[]> = ((...t: T) => any) extends ((
first: any,
...rest: infer Rest
) => any)
? Rest
: never;
// use a distributed conditional type here
type ShiftUnion<T> = T extends any[] ? Shift<T> : never;
Test
type DeepRequiredExample = DeepRequired<
Example,
["a", "b", "c"] | ["one", "two", "three"]
>;
declare const ex: DeepRequiredExample;
ex.a.b.c; // (property) c: number
ex.one.two.three; // (property) three: number
ex.one.two.four; // (property) four?: number | undefined
ex.always // always: number
ex.example // example?: number | undefined
Playground
Some polish (Update)
There is still some minor inaccuracy left: If we add property two
also under a
, e.g. a?: { two?: number; ... };
, it also gets marked as required, despite not beeing in our paths P
with ["a", "b", "c"] | ["one", "two", "three"]
in the example. We can fix that easily by extending the ShiftUnion
type:
type ShiftUnion<P extends PropertyKey, T extends any[]> = T extends any[]
? T[0] extends P ? Shift<T> : never
: never;
Example:
// for property "a", give me all required subproperties
// now omits "two" and "three"
type T = ShiftUnion<"a", ["a", "b", "c"] | ["one", "two", "three"]>;
= ["b", "c"]
This implementation excludes equally named properties like two
, that are in different "object paths". So two
under a
is not marked required anymore.
Playground
Possible extensions
- For single required properties pass in strings instead of tuple paths for convenience.
- Current implementation is suitable for a few object paths to be marked required; if multiple nested sub properties from an object are to be selected, the solution could be extended to receive object literal types instead of tuples.
Hope, that helps! Feel free to use that as a base for your further experiments.
"a.b"
(just a string) into meaning nested properties, so if that is part of what you're looking for (and I think it is), the answer is no. If you're more flexible on the syntax it's a different story.MarkRequired<Example, { a: { b: { c: any } }; one: { two: { three: any } } }>
, let me know and I'll post an answer.a.b
syntax, the question was more about markinga
as required and then having properties under it also marked as required. Thanks.