Given a type like:
type Book = {
__typename?: 'Book';
author?: {
__typename?: 'Author';
name?: string;
};
};
I want to remove the optional flag from the __typename properties, recursively. Resulting in:
type TypedBook = DeepMarkRequired<Book, "__typename">;
// type TypedBook = {
// __typename: 'Book';
// author?: {
// __typename: 'Author';
// name?: string;
// };
// };
ts-essentials has MarkRequired, but it's not recursive, and when I try a simple implementation like:
type DeepMarkRequired<T extends object, K extends PropertyKey> = MarkRequired<
{ [P in keyof T]: T[P] extends object ? DeepMarkRequired<T[P], K> : T[P] },
K
>;
TypeScript complains on line 3 that K doesn't satisfy keyof T, which makes sense since Required and Pick both require keyof T rather than PropertyKey to allow this to be generic. I'm not sure how to allow passing an arbitrary parameter name all the way down the tree.