Skip to content

Instantly share code, notes, and snippets.

@prmichaelsen
Created May 16, 2019 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prmichaelsen/39f4381dbe7ce617870028485aa74c3f to your computer and use it in GitHub Desktop.
Save prmichaelsen/39f4381dbe7ce617870028485aa74c3f to your computer and use it in GitHub Desktop.
recursively mark all properties of a typescript type as defined
/** private type, not exported */
declare type NonObject = undefined | null | boolean | string | number | Function;
/**
* This type allows you to mark an object with
* optional properties as required.
*/
export declare type Complete<T> = {
[K in keyof T]-?: T[K];
}
/**
* This type allows you to mark an object with
* optional properties as required recursively.
*/
export declare type DeepComplete<T> =
T extends NonObject ?
T extends undefined ? never : T
:
T extends Array<infer U> ?
DeepCompleteArray<U>
:
T extends Map<infer K, infer V> ?
DeepCompleteMap<K, V>
:
DeepCompleteObject<T>;
interface DeepCompleteArray<T> extends ReadonlyArray<DeepComplete<T>> { }
interface DeepCompleteMap<K, V> extends ReadonlyMap<DeepComplete<K>, DeepComplete<V>> { }
declare type DeepCompleteObject<T> = {
[K in keyof T]-?: DeepComplete<T[K]>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment